tags:

views:

392

answers:

2

Hi.

I have the following pretty standard process:

  1. I initialise my page in $(document).ready. This includes binding events to elements in a table.
  2. The content of the table is then refreshed dynamically through an ajax call

I now need to rebind events to the table content. Is there a standard way of doing this? i.e. is there an equivalent of $(document).ready that fires after a partial page DOM update?

Thanks.

+14  A: 

Have a look at event delegation using live. From the docs:

When you bind a "live" event it will bind to all current and future elements on the page (using event delegation). For example if you bound a live click to all "li" elements on the page then added another li at a later time - that click event would continue to work for the new element (this is not the case with bind which must be re-bound on all new elements).

An example:

$('#myTable td').live("click", function() {
    alert('hello!');
});

That will preserve the event(s) bound to the cells on the table even after it has been replaced. The live manual says:

Binds a handler to an event (like click) for all current - and future - matched element.

Alternatively, you can wrap your bindings into a function, and have that execute as a callback to the method that updates your table data, for instance:

function bindStuffToTable()
{
    $('#myTable td').click(function() {
       alert('Hello!');
    });
}

$('#myTable').load('/some/link', bindStuffToTable);

Or if the entire table gets replaced dynamically, which is more likely:

$('#someButton').click(function() {
    //replaces the contents of someDiv with the table generated by foo.php
    $('#someDiv').load('foo.php', bindStuffToTable);
});
karim79
Just to note that if you don't see any of the live stuff working, make sure your using the latest version of jQuery.
Tom
Thanks for the detailed answer. I looked at live events and also at the live plugin (which gives additional functionality). In the end I went with having a function that handled the rebinding - felt a bit less "smoke and mirrors". Thanks again.
Nick Pierpoint
+1  A: 

I use $(document).ready() again if I receive a partial update...

This is the AJAX response

<script type="text/javascript">
    $(document).ready(function() {
        alert($('div.partialresponse').html());
        //Alerts 'content'
    });
</script>
<div class="partialresponse">
   content
</div>
Ropstah
How does this work? Does the new "ready" function replace the one that was created on the intial page load?
Nick Pierpoint
I guess... The old one has executed already so it isn't needed anymore right? :)
Ropstah
... but if the original $(document).ready() function created and initialised other functions - what happens to them? A quick test in IE6 seems to show they still remain, but it does feel a bit indeterminate.
Nick Pierpoint
You store those functions -outside- the document ready function right? E.g. inside a DOM element. Functions are passed by value, not reference. So if you -delete- the original ready() function the function definitions inside which you passed on before will not be deleted with it...
Ropstah
@Nick: Then those functions would continue to exist in the enclosure of the lamba passed to that 'first' `$(document).ready()` call. Any processing in that first `$(document).ready()` is already done - leaving only variable and function definitions (which, as I say, will persist in the enclosure).
Chris
Thanks @Chris - that makes sense.
Nick Pierpoint