views:

367

answers:

2

i have many events such as

$('.hidefile').click(function (event) {

On page load and on certain conditions i use ajax to get the html for a certain div and fill it in with .html(htmlstring). It took me forever but i notice that the events do not work when i do this. How do i fill the div with html and have the jquery events work with it?

+8  A: 

Bind your click events (or otherwise) to your elements using live:

$('.hidefile').live("click", function (event) {...

When you replace elements using ajax, they lose their event handlers. You can either explicitly rebind them within the success callback of the particular ajax method you have used to replace those elements, or use live handlers instead, which will:

Attach a handler to the event for all elements which match the current selector, now or in the future.

karim79
One of the problems of being old is that you read the docs a long, *long* time ago and then you miss all sorts of nice new features that appeared while you weren't looking. Thanx, karim79!
Peter Rowell
A: 

How are you attaching the click event. You should do that inside the document.ready event to make sure that the DOM has been loaded. Also, as suggested by karim79 you should use the live function to make sure that the events persists.

azamsharp