TOC
Need to learn JavaScript? jQuery is a JavaScript framework, so if you don't already know about the JavaScript programming language, we recommend that you learn it now: Learn JavaScript

The community is working on translating this tutorial into Turkish, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Events:

The live() method

In the previous chapters, we used the bind() and unbind() methods to attach and detach event handlers to various elements on the page. This works great for elements which already exists, but what if you want your event handler to be attached to future elements as well? Normally you would have to do this manually, upon creating the new elements, and this is still possible. However, using the live() method, you can inform jQuery to attach your event handler to any future elements which matches your original selector, without having to lift a finger. Let me first show you an example where we use the bind() method, and then replace it with the live() method, to show you the difference:

<div id="divTestArea1">
	<a href="javascript:void(0);" onclick="AddBox();">Add box</a>
	<div class="test">This is a box</div>
</div>

<script type="text/javascript">
$(function()
{
	$(".test").bind("mouseover", function()
	{
		$(this).css("background-color", "blue");
	}).bind("mouseout", function()
	{
		$(this).css("background-color", "white");
	});
});

function AddBox()
{
	var div = $("<div></div>").addClass("test").text("Another box");
	$("#divTestArea1").append(div);
}
</script>

Okay, this example might seem a bit complicated, but actually it's not. Let me walk you through it. We have a link, which will call the AddBox() JavaScript method, and then we have a div with the class "test". The AddBox() method will simply add another div to the page, with the same class, so that when you click the link, you get yet another box on the page. In the ready event, we select all elements with the "test" class and then we bind a handler to two of the events: The mouseover and the mouseout event, where we change the color of the element invoking the event. Try the example in your browser. The first div will have the mouseover effect, but if you click the link to add more boxes, they won't have the same effect. The reason is pretty obvious: We attached the events before these new boxes were created.

Now try the following example instead. I have only changed two words in it: The two calls to bind() has been replaced with calls to live():

<div id="divTestArea2">
	<a href="javascript:void(0);" onclick="AddBox();">Add box</a>
	<div class="test">This is a box</div>
</div>

<script type="text/javascript">
$(function()
{
	$(".test").live("mouseover", function()
	{
		$(this).css("background-color", "blue");
	}).live("mouseout", function()
	{
		$(this).css("background-color", "white");
	});
});

function AddBox()
{
	var div = $("<div></div>").addClass("test").text("Another box");
	$("#divTestArea2").append(div);
}
</script>

Now if you run this example, you will see that even though you add new elements after the page has loaded, jQuery will automatically attach the event handlers to them for you. The live() method works just like bind() in all the other aspects, so check the previous chapters for more information on it. The same goes for the die() method, which works just like the unbind() method, but should be used for cases where the live() method has been used.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!