jquery - Why doesn't click work on appended elements? -
i move html elements 1 container endlessly using jquery append
function but, click event won't fire no more when click on element/s have been appended.
based on threads similar mine found out appended elements stripped off of event listeners. how can avoid that, can show solution ?
here the: fiddle
$('section.container-a div.elem').click(function() { $('section.container-b').append(this) ; }) ; $('section.container-b div.elem').click(function() { $('section.container-a').append(this) ; }) ;
it work. use following method appended.
$(document).on('click', 'section.container-a div.elem', function() { $('section.container-b').append(this) ; }) ;
explanation of problem,
doing following,
$("span").click(function(){
an event handler attached span
elements currently on page, while loading page. create new elements every click. have no handler attached. can use
$(document).on('click', 'span.class', function(...
that handle clicks on new elements well.
Comments
Post a Comment