thornomad thornomad - 2 years ago 116
Javascript Question

Finding child elements of a cloned element in jQuery

I don't know why I'm stuck on this but I am! Trying to clone a

and then modify its contents using
in jQuery. I am missing something here because it's not working as I would expect. See fiddle:

Javascript (jQuery):

$test = $('#clone-container').clone().appendTo('#append');
$test.children('h2').text('my clone'); // works
$test.children('.remove-row').remove(); // doesn't work

And the HTML:

<div id="clone-container" class="hidden">
<h2>Location name</h2>
<div class="table-responsive">
<table class="table">
<tr class="remove-row"><td colspan="6">Remove this text from clone</td></tr>
</div> <!-- .table-responsive -->
<div id="append"></div>

Answer Source


.remove-row is not a direct child of the cloned element. Replace this:


with this:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download