Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make live custom events in jQuery

jQuery has a really handy event binder called live() which will add events to DOM elements on the fly (even for the elements that will be added later to the DOM). The problem is that it's only working on specific events (listed here in documentation).

I really want to have live events for focus,blur and change which is not supported by live right now. Besides, if I can make live custom events, it will be big game changer for my app. Most of the code that I have right now is dedicated to rebinding old events (change, focus, and custom events for making items draggable or resizable) to new dom elements that have been added through ajax.

Any idea? I guess event delegation is the way to go, but I right now it'll make the code more complicated. Maybe a plugin that handle event delegations... not sure. Help me find a solution.

like image 988
Allen Bargi Avatar asked May 02 '09 22:05

Allen Bargi


2 Answers

This functionality is now available in jQuery 1.4. live() now supports all JavaScript events (including custom events), and the focusin and focusout events have been introduced as bubbling versions of focus and blur.

From the jQuery 1.4 documentation on .live():

As of jQuery 1.4, the .live() method supports custom events as well as all JavaScript events. Two exceptions: Since focus and blur aren't actually bubbling events, we need to use focusin and focusout instead.

like image 115
PCheese Avatar answered Sep 21 '22 22:09

PCheese


If it's not in jQuery there is most likely a reason. Browser bugs etc that make it unreliable. I would wait until they implement it or try using the original plugin that became live http://docs.jquery.com/Plugins/livequery

Edit:

Nice downvote guys. There is a reason it's not in jQuery and I highly doubt it's because they're lazy. I've actually spent time reading the source and looking for why only certain events are implemented in live() and I can't find why. So if someone knows ... please enlighten us.

like image 29
Chad Grant Avatar answered Sep 20 '22 22:09

Chad Grant