How can I add jQuery click events to a page which was loaded with XMLHttpRequest?
1 Answer
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
});
6 Comments
testkhan
could you guid me something about $.live
testkhan
but how can i combine and external function to $.live() with adding any event suppose i have a function like function myfnc(){ alert("Done"); } than how can i combine this to $.live on window load......
Sampson
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 :)
testkhan
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()
Sampson
$.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. |