$('#buttonid').click()
to attach a handler function which can display alert message. But what if we dynamically add another button on page and we want exactly the same functionality? Well, jQuery Live Function comes to rescue for such requirements. Let us see an example of normal event handling and one with Live function. In this example, we are dynamically adding list items that has hover effect. When mouse is hover on this list item, it changes the color. We will do this by both traditional way and using Live function. <TABLE>
<TR valign="top">
<td>
<input type="button" value="Click To Add (without Live)" id="add"/>
<ul id="list" class="list">
<li> </li>
<li> </li>
<li> </li>
<li> </li>
</ul>
</td>
<td>
<input type="button" value="Click To Add (with Live)" id="addLive"/>
<ul id="listLive" class="list">
<li> </li>
<li> </li>
<li> </li>
<li> </li>
</ul>
</td>
</TR>
</TABLE>
Code language: HTML, XML (xml)
The HTML will display two buttons and two list sets. One button will dynamically add list item to one list using normal click event, where as another button will use Live events to do this. .list li {
list-style: none;
background-color:#eeffaa;
width:150px;
margin-top:5px;
border:1px solid #8AAC00;
}
.list li.selected {
background-color: #ffaaee;
}
Code language: CSS (css)
$('#add').click(function(){
$('#list').append('<li> </li>');
});
$('#addLive').click(function(){
$('#listLive').append('<li> </li>');
});
Code language: JavaScript (javascript)
Then, we will make the first list hoverable using mouseover and mouseout event. $('#list>li')
.mouseover(function(){ $(this).addClass('selected'); })
.mouseout(function(){ $(this).removeClass('selected'); });
Code language: JavaScript (javascript)
And, then we will make the second list hoverable using live()
event. $('#listLive>li')
.live('mouseover', function(){ $(this).addClass('selected'); })
.live('mouseout', function(){ $(this).removeClass('selected'); });
Code language: JavaScript (javascript)
Notice the difference in above code. In first snippet we are using mouseover and mouseout functions to attach events with handlers to change the background color of list item. When we add a list item dynamically, this handler are not attached to newly created list item. And in second snippet we are using live() to attach event mouseover and mouseout. This way we ensure that any dynamically added element will have the events handler attached with them. jQuery 1.3 and below Currently jQuery 1.3 and below is not supporting events blur, focus, mouseenter, mouseleave, change, submit. But the good news is that jQuery 1.4 alpha 1 has just been released which supports these events. stopPropagation
or stopImmediatePropagation
.$("li a").live(...)
but this would not: $("a", someElement).live(...)
and neither would this: $("a").parent().live(...)
.die()
methodJava URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Hi,
U have a nice post!
It is so interesting and realistic !
Thanks for sharing !
Very nice and useful tutorials to web designers,
Thanks for posting.
Excelent example
we can do it with .clone(true) function
Great post . I encounter a problem with live event.
The senario is like this .I am using displaytag inside a jquery tab , displaytag generate a dynamic navigation link bar , when I click the link , the page refreshed .
So , I want to control the link and change the link to go through my adapter function to do an ajax load without page refresh.
Cause the links are generated dynamically , so I want to bind them to live event .
$("pagelinks a['href*=page']").live('click' , function () { // do somethings });
The problem is the selector returns a collection , do I need to bind a collection of similar tag to live events ? This is no any difference to bind them to click directly through .each loop.
hey very nice article.
i was very confused with live in sense what exactly its use is.
this article made the picture clearer.
thanks.
Nice tutorial
yep, when i first learnt jquery i had problem on binding event in dynamically added elements.
people at stackoverlfow.com helped me a lot.
And here you share the knowledge
hope more people will understand this concept.
[code language="java"]
System.out.println("You Rock :D");
[/code]
Hi
Thanks for very clearly explaining the 'Live' event. Enjoyed reading it!