jQuery 1.3 came out on January 14th, jQuery 1.3.1 on the 21st, and with them we now have live events built into jQuery.
Live events are pretty magical at first glance. They allow you to set events only once, and they work forever in the future, even as you're creating new elements and adding them to the page.
Normally if you ran:
$('a.wizard').click(function(){
// do some wizardry
});
and then later you added wanted to add some more <a class="wizard">s to the page, you would have to re-attach this event handler over and over.
Live events allow you to add an event that will work forever. This means you only have to add each type of event once. You would only have to write:
$('a.wizard').live('click', function(){
// do some wizardry
});
And your wizard links will work forever, even after you add 100 new wizard links to the page dynamically.
This magic trick works by attaching the click event to the document. Whenever you click anywhere on the page, the document click event gets called. jQuery compares the target element to your wizard links and triggers your click event if the click came from inside the link.
You can also do this yourself using the new closest() function. It allows you to do something like this:
// listen for clicks on the document
$(document).click(function(e){
// look for a possible parent element matching a.wizard
$(e.target).closest('a.wizard').each(function(){
// wizard it up
});
});
These live events can really help with performance. If you're attaching events to 100s of similar elements, like photos in an album, you can also save a lot of memory and speed things up by using live events, or using the example above and checking for events on a common parent element, either document or any element.
If you're used to using closures to use data within click handlers, you will find they won't work anymore which is probably a good thing. Instead, you can use data() to store any amount of data with that element and get it out later:
// maybe this is some JSON data you got using Ajax or something
var wizards = [
{ name: 'Merlin', skill: 'magic' },
{ name: 'Mr. Wizard', skill: 'science' }
];
$.each(wizards, function(i, wizard){
// create a new link, change the text, add the data and append to the body
$('<a class="wizard"/>')
.text(wizard.name)
.data('wizard', wizard)
.appendTo(document.body);
});
then you only need to attach the click handler once:
$(document).ready(function(){
$('a.wizard').live('click', function(){
// fetch the data back out
var wizard = $(this).data('wizard');
// get the stuff you need out of the object
var name = wizard.name;
var skill = wizard.skill;
// do your thing
alert(
"Hello my name is "
+ name
+ "and I'm better than you at "
+ skill
+ "!!!"
);
});
});
And you can actually do these things in reverse, the order doesn't matter because of the magic and universality of live events.
Pretty cool, eh? This way of developing has always been possible using JavaScript, but after learning about this with jQuery 1.3, it changed the way I look at programming with data and events.
What do you think? Any questions, corrections or suggestions? Leave a comment.
One should note that 'live' can only be bind to events, which means LiveQuery (http://plugins.jquery.com/project/livequery) is still the more flexible way of doing live updates, because it can be bind to pretty much any jQuery DOM objects.
I think that you have a really good name for your webpage, but you need someone who helps you in the desing!!!!!!!!!!
I know a really good one... his email is proter-@hotmail.com
Not to derail this post but..
@ :) i :) I'm just a visitor, but I believe the site is rather professionally looking as it is...
thanks for pointing this out, it's cool that this is part of the core of jquery now, i did not know that.
I'm just trying to learn to do some stuff with jQuery, i've never done any javascripting before so it's a steep learning curve but I think a good way in.
For awhile there e.stopPropagation was broken when using jQuery.live, which is logical the way event delegation works in jQuery, but it was supposedly fixed in the latest release. Haven't check out how they did it, but am glad they found a way.
Live events are very helpful especially in my last project where I had to bind events at runtime.
Hi. Jesse, i need help about , which one is better framework (between mootools and jquery) and why. thanks
pd. interesting articles. :D
@Dan - I'd say jQuery is better but I'm extremely biased and have never used mootools. So maybe you should ask around first :)
Thanks for the neat "coverage" of live events ;).
According to Yahoo! recommendations, script links should be placed in the end of the page, but as I can see, this plugin sends only ‘head’ child tags, doesn’t it?
So why did I read about LiveQuery recently?!
Using a feature which is "built in" seems to be better (usually).
I want users of my site to be able to record and play sound. What would be a best option? Flash? Thank you in advance.
@Dan - mootools is not so bad, it is bit lame librarary.
If you are using it in combination with ASP.NET AJAX you can have a lot headaches :)
@Jesse i need help plzzzz I have nothing to get from page but i want that whenever any hyperlink on page is clicked it gets count .... same as happening in site statistics software like i dont have any access to body and any element what i can do is just paste a script in head and secondly i am not aware while webpage is ajax enabled or not
@Julia - live events should work well for this. You can do:
$('a').click(function(){
// track link
});
I want to hide a specific div tag from an Asynchronously Loading Webpart, is it possible to use the .live() for this? If yes, how? Thanks.
@Marlon - I'm not sure, but it sounds like CSS would do the trick. If the div has a class of dont_show_me then you can have CSS like:
.dont_show_me { display: none }