Jonas Skovmand,

Web developer from Sweden, Denmark and Norway.

Revent all ye jQuery scripters

*** NOTE *** After jQuery 1.3 introduced Live there is no need for this function.

I recently wrote a small plugin for jQuery for simple reattaching of events on newly added content to the DOM (i.e. from an AJAX request).

The plugin, named Revent (both as a pun on the word repent, and as a contraction of re and event, yes I’m silly), is basically removes an the event in question and adds it again – albeit roughly but effective.

The syntax is as follows:

$( selector [, scope]).revent ( string event helper, function callback  )

And a simple example to demonstrate:

function revent () {
   $('.box').revent ( 'click', function ( ) { doSomething(); } ); // the magic occurs here!
}
 
function addBoxes ( ) {
    // add some boxes to the DOM or whatever
    revent(); // calls revent function that rebind events!
}
$(document).onload ( function ( ) {
    $("#next-page").click ( function ( ) { addBoxes ( ); } );
 
    revent ( ); // bind events for the initially existing links
});

For more in-depth documentation, see the example/demo page.

Download jQuery Revent: source (development) | minified (production)

(0) (0)

Leave a Reply