user2507316
user2507316

Reputation: 169

jQuery how to add handler to new appeared elements

$(function(){

$('#somelement').click(function(){


  //do stuff

});


});

This code don't work on elements that have appeared a bit later, how to make this bind to all elements that will appear in the future

Upvotes: 0

Views: 849

Answers (1)

Use .on()

As your content is added dynamically so it is not accessible directly ,So you have to use Event delegation.

$(document).on('click','#somelement',function(){

});

or

you can bind the Event delegation to the parent element of the new element which is present in DOM at the of time DOM ready or page load.

$('#parentID').on('click','#somelement',function(){

});

Upvotes: 3

Related Questions