Skip to content Skip to sidebar Skip to footer

How To Bind A Function To Element Loaded Via Ajax

I have a couple of Divs in my page with CSS class='hello' Further I use Ajax to fetch a few more Divs with a CSS class='hello' I have a piece of code which is called on Click event

Solution 1:

you should use .on to bind the handler to another element that is already present during the execution of the binding, like say the <body>, or the document and have it detect the children events. but ideally, you should bind it to the nearest common parent of the content loaded.

demo

//bind to the body a "click" handler for elements that have class names of "hello"
$('body').on('click','.hello',function(){
  alert("Hello Clicked");
})

Post a Comment for "How To Bind A Function To Element Loaded Via Ajax"