tags:

views:

26

answers:

1

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
could you guid me something about $.live
testkhan
thanks a lot Jonathan Sampson i have done that thanks a lot.....!!
testkhan
Please feel free to accept the solution if it worked.
Jonathan Sampson
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
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
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
`$.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
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