jquery - Events on cloned elements? -
jquery - Events on cloned elements? -
i have on click:
$('.btn-delete').on('click', function(){console.log('delete')});
i clone div:
this.filetemplate = $('.file:first').remove().clone(true);
later add together clone page.
'delete' fails log
the html:
<li class="file"> <button class="btn-delete">×</button> </li>
since you're not cloning button itself, parent, need deep clone:
this.filetemplate = $('.file:first').clone(true,true); $('.file:first').remove();
http://api.jquery.com/clone/#clone-withdataandevents-deepwithdataandevents
however, if you're removing element anyway, don't need clone @ -- store div of events using .detach()
instead of .remove()
:
this.filetemplate = $('.file:first').detach();
http://api.jquery.com/detach/
to add together copies of element, deep-clone after it's detached:
clone_copy = this.filetemplate.clone(true,true); clone_copy.appendto('#container');
jquery
Comments
Post a Comment