Over a million developers have joined DZone.

Using jQuery Live instead of jQuery Hover function

·

Let’s say we have a case where we need to create mouseover / mouseout functionality for a list which will be dynamically filled with data on client-side.

We can use jQuery hover function, which handles the mouseover and mouseout events with two functions.

See the following example:

<!DOCTYPE html>
<html lang="en">
<head id="Head1" runat="server">
<title>jQuery Mouseover / Mouseout Demo</title>

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.4.js"></script>
<style type="text/css">
.hover { color:Red; cursor:pointer;}
</style>
<script type="text/javascript">
$(function () {
$("li").hover(
function () {
$(this).addClass("hover");
},
function () {
$(this).removeClass("hover");
});
});
</script>
</head>
<body>
<form id="form2" runat="server">
<ul>
<li>Data 1</li>
<li>Data 2</li>
<li>Data 3</li>
<li>Data 4</li>
<li>Data 5</li>
<li>Data 6</li>
</ul>
</form>
</body>
</html>

Now, if you have situation where you want to add new data dynamically... Lets say you have a button to add new item in the list.

Add the following code right bellow the </ul> tag

<input type="text" id="txtItem" />
<input type="button" id="addNewItem" value="Add New Item" />

And add the following button click functionality:

//button add new item functionality
$("#addNewItem").click(function (event) {
event.preventDefault();
$("<li>" + $("#txtItem").val() + "</li>").appendTo("ul");
});

The mouse over effect won't work for the newly added items. Therefore, we need to use live or delegate function. These both do the same job. The main difference is that for some cases delegate is considered a bit faster, and can be used in chaining.

In our case, we can use both. I will use live function.

$("li").live("mouseover mouseout",
function (event) {
if (event.type == "mouseover") $(this).addClass("hover");
else $(this).removeClass("hover");
});

The complete code is:

<!DOCTYPE html>
<html lang="en">
<head id="Head1" runat="server">
<title>jQuery Mouseover / Mouseout Demo</title>

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.4.js"></script>
<style type="text/css">
.hover { color:Red; cursor:pointer;}
</style>
<script type="text/javascript">
$(function () {
$("li").live("mouseover mouseout",
function (event) {
if (event.type == "mouseover") $(this).addClass("hover");
else $(this).removeClass("hover");
});

//button add new item functionality
$("#addNewItem").click(function (event) {
event.preventDefault();
$("<li>" + $("#txtItem").val() + "</li>").appendTo("ul");
});
});
</script>
</head>
<body>
<form id="form2" runat="server">
<ul>
<li>Data 1</li>
<li>Data 2</li>
<li>Data 3</li>
<li>Data 4</li>
<li>Data 5</li>
<li>Data 6</li>
</ul>


<input type="text" id="txtItem" />
<input type="button" id="addNewItem" value="Add New Item" />
</form>
</body>
</html>

So, basically when replacing hover with live, you see we use the mouseover and mouseout names for both events.

Check the working demo which is available HERE.

UPDATE (12.02.2011 01:05AM CET)


NOTE: As Mr. Steve Wellens noted in the comments, the jQuery LIVE function can be used to connect handler to any event for the given selector. In some cases I prefer using DELEGATE function, but lets leave it for another topic. So, in order not to make confusion, the blog shows one useful way to replace HOVER with LIVE for a situation where HOVER cannot satisfy our needs. So, you can add additional events to which you want the LIVE function to connect the handle. See the following example:

$(function () {
$("li").live("mouseover mouseout click",
function (event) {
if (event.type == "mouseover") $(this).addClass("hover");
else if (event.type == "mouseout") $(this).removeClass("hover");
else $(this).toggleClass("clicked");
});

Besides the mouseover and mouseout, I added click event also. Then I'm just checking the type. If you test this, also define .clicked { background-color:Yellow; } class in your CSS ;).

In this example I only show changing of colors and working with jQuery CSS-related classes. You can perform additional operations by writing your own JavaScript / jQuery code.
Topics:

Published at DZone with permission of Hajan Selmani, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}