jQuery 1.3 was released and the most useful new feature is the live event!

Previously in older versions when you dynamically added new DOM elements into you page your event handlers would not automatically be attached to those elements.

You run into this issue a lot if you use AJAX or are creating lots of elements on the fly. The only trick before this new feature was to rebind all those events whenever a new element was added.

With the live event, it automatically binds up all current and future elements matching the selector

So instead of using:

// Will only add the event handler to current matched elements in the DOM
$("div").click(function() { doSomething(); });

You can use

// Will add the event handler to all current and future matched elements in the DOM
$("div").live("click", function() { doSomething(); });

Check it out, it’s a great new feature.

Comment