Algemeen en interessant nieuws over en door NosTech


jQuery: Prepend event handlers
27-03-2015 23:24

Although it’s not integrated by default in jQuery, it’s pretty easy to prepend a new handler to the existing handlers for a certain event in jQuery. In the following example we will prepend a new click-event to the existing ones:

Add new handler to click event:

Now move it to the first position:
var lastEvent = $._data($(’element’).get(0), ‘events’).click.pop();
$._data($(’element’).get(0), 'events’).click.unshift(newEvent);

PS: If you have a selector which can have multiple elements selected, make sure to put this code in a loop and replace ‘.get(0)’ by ‘.get(counter)’.

< Alle nieuws