How can I add jQuery click events to a page which was loaded with XMLHttpRequest?
+1
A:
Use the $.live()
method to handle items that will appear later in the pages lifetime.
$("a").live("click", function(e) {
e.preventDefault(); // disables all links, even links added via AJAX
});
Jonathan Sampson
2009-12-01 18:01:13
could you guid me something about $.live
testkhan
2009-12-01 18:03:34
thanks a lot Jonathan Sampson i have done that thanks a lot.....!!
testkhan
2009-12-01 18:05:59
Please feel free to accept the solution if it worked.
Jonathan Sampson
2009-12-01 18:06:23
but how can i combine and external function to $.live() with adding any event suppose i have a function likefunction myfnc(){alert("Done");}than how can i combine this to $.live on window load......
testkhan
2009-12-01 18:16:58
You should keep your functions out of your external pages. Keep your javascript in javascript files, html in html files, php in php files, and so on :)
Jonathan Sampson
2009-12-01 18:22:39
i am using elastic() jquery plugin to make my textarea behaves auto height......i am implement it liek$(document).ready(function(){$("#textarea").elastic();});where #textarea is id of my textarea....so how can i implement this in $.live()
testkhan
2009-12-01 18:42:52
`$.live()` only works for a limit set of events: click, doubleclick, mousedown, etc. For this reason, you won't be using `$.live()` to apply `$.elastic()` to your textareas. Instead, you'll have to manually apply it after each ajax-request finishes. Look for new textareas, and apply it to them.
Jonathan Sampson
2009-12-01 20:10:54
Your question here was "How do I add click events to dynamically loaded elements." My answer explains how - as such, if you have other issues, you should create new questions.
Jonathan Sampson
2009-12-01 20:11:55