JQuery event for Ajax response

How to run jquery function for .click() event for an element created using ajax request?

It works for a normal element, but I want to do this for an element created using an ajax request. And it does not work ...

 $(".links").click(function(){alert("aaaa");}) a class="links" >1</a; 

I also tried using "links" as id. I need this for a pagination system.

Please, help: (

+4
source share
4 answers

You need to run the code again to bind the click event handler after adding a new element to the DOM. Alternatively, you can use . Live () .

+4
source
+1
source

use jquery live,

 $(".links").live('click', function(){ //user code here }); 

To work with dynamically loaded doms, you must use the live function. jquery does not load pages automatically after page loading.

+1
source

You should use live () as follows:

 $(".links").live('click', function(){alert("aaaa");}) 

This also works with elements added to the document.

for onclick for each <a> element you should do this (if that's what you mean:

 $("a").live('click', function(){ alert($(this).attr("id")); }) 

This binds the event to all <a> elements, as well as to those added subsequently.

EDIT - now the link identifier is displayed in the message: 'this' in this case refers to the <a> that was pressed

+1
source

All Articles