Vivek P
Vivek P

Reputation: 3050

Wrap an HTML structure around text using jquery

I was trying to wrap a Achor tag to the Email-id dynamically, I am facing issues below is my html code:

<tr>
  <td id='label'>
    email
  </td>
  <td>
    <div>
      [email protected]
    </div>
  </td>
</tr>

and jquery code which i am trying is wraping the Achor tag to the div :

$("tr td#label:contains('email')").next().each(function(){ 

   $(this).wrapInner('<a href="mailto:'+$(this).text()+'">');

});

i what to wrap a Achor tag to Email-Id. how can i do this?

Upvotes: 0

Views: 108

Answers (1)

YD1m
YD1m

Reputation: 5895

First modify markup to use class insted of id :

<tr>
  <td class='label'>
    email
  </td>
  <td>
    <div>
      [email protected]
    </div>
  </td>
</tr>

Script:

$(".label:contains('email')").each(function(){
    var email = $(this).closest('td').next('td').find('div').text();
    $(this).html('<a href="mailto:'+email+'">email</a>');
});

Upvotes: 1

Related Questions