Shad Gagnon Shad Gagnon - 5 months ago 12
jQuery Question

How to merge parent content inside his first child with jQuery

I'm not able to figure out how to merge parent content inside his first child with jQuery.
I want to merge all the elements (including text node) of a element within its

<a>
child.

I have this :

<td class="row">
<a href="#">
<span class="iconouter">
<img class="icon" src="...">
</span>
Type of document
</a>
: Form
<span class="type-nb">(1)</span>
</td>


And I want to do this :

<td class="row">
<a href="#">
<span class="iconouter">
<img class="icon" src="...">
</span>
Type of document: Form <span class="type-nb">(1)</span>
</a>
</td>

Answer

Finally, I found a solution using jQuery contents() function :

$('.row').each(function() {
    $(this).find('a').append($(this).contents()[1], $(this).contents()[2]);
});

It's not very flexible, but it suits my needs. Thank you all!

Comments