FlyingCat
FlyingCat

Reputation: 14270

How to remove html tag and wrap it with another tag

I am trying to remove the some elements under a table cell.

my html is like

<table>
<tr>
  <td>
     <span>first text</span>
     <span>second text</span>
     <span>third text</span>
  </td>
   <td>
     <span>1st text</span>
     <span>2nd text</span>
     <span>3rd text</span>
   </td>
</tr>
…more

I want my html become

<table>
<tr>
  <td>
     <span>first text second text third text</span>
  </td>
   <td>
     <span>1st text 2nd text 3rd text</span>
   </td>
</tr>
…more

I have tried.

$('table td span').contents().unwrap().wrap('<span></span>');

but I got the same results as my original one.

Are there anyways to do this? Thanks a lot!

Upvotes: 2

Views: 103

Answers (1)

Jason P
Jason P

Reputation: 27012

Need to iterate each td and use wrapAll instead of wrap:

$('table td').each(function() {
    $(this).find('span').contents().unwrap().wrapAll('<span></span>');
})

http://jsfiddle.net/gNMCh/

Upvotes: 5

Related Questions