Wednesday, 18 September 2013

jQuery event won't work on dynamically fetched HTML

jQuery event won't work on dynamically fetched HTML

Suppose I want to change the innerHTML of all span tags when they are
clicked that have the attribute 'data-change':
$('span[data-change]').click(function(){
$(this).text('Text was changed dynamically');
});
Now, if I add a new span tag with data-change attribute inside my html
document using jQuery on javascript, the onclick event won't work on the
newly added span tags. Why? And how can I make them work? Thanks!

No comments:

Post a Comment