PS: Updated Fiddle
Here's a fiddle that wraps text in elements
Here's a fiddle trying to split text using commas as delimiters and wrap each of the words in <span class="new"/>
. Would anyone please tell me why it gives me Uncaught TypeError: undefined is not a function
in the foreach loop?
I want the output to be
<td>
<span class='new'>Text</span>
<span class='new'>that</span>
<span class='new'>needs</span>
<span class='new'>to</span>
<span class='new'>be</span>
<span class='new'>wrapped.</span>
<span class="count">not this</span>
<button>Wrap</button>
</td>
Code:
$('button').click(function(){
var wrap_it = $(this).closest('td').contents().eq(0).text();
wrap_it = wrap_it.split(',');
console.log(wrap_it);
$.each(wrap_it,function(i,k){
k.wrap('<span class="new"/>');
})
});
HTML:
<table>
<tbody>
<tr>
<td>1</td>
<td>Text,that, needs, to, be, wrapped.<span class="count">not this</span><button>Wrap</button>
</td>
</tr>
<tr>
<td>2</td>
<td>Text, that, needs, to, be, wrapped.<span class="count">not this</span><button>Wrap</button></td>
</tr>
</tbody>
</table>
Also made a try
With Fiddle: Wrap splitted text
I made a try:
fiddle
Tell me please if it suits you.