jQuery and ASP.NET

November 17, 2009

When to use the jQuery live() and bind() methods




I get asked occasionally when to use the bind() and live() jQuery event methods. bind() will bind a handler to one or more events for each matched element, at the time the call is made; whereas live() uses event delegation to bind a handler to an event for all current and ‘future’ matched elements. If you have controls that aren’t rendered dynamically, use bind(). If you have controls that are rendered dynamically, use live().

Here are two examples.

Bind

<html xmlns="http://www.w3.org/1999/xhtml" >
<
head>
<
title></title>
<
script language="javascript" type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</
script>
<
script language="javascript" type="text/javascript">
$(function() {
$("input[type=button]").bind("click", function() {
$(this).after("<br /><input type=\"button\" value=\"Child\" />");
});
});
</script>
</
head>
<
body>
<
form>
<
input id="btnCreate" type="button" value="Create Children" />
</
form>
</
body>
</
html>

In the code above, when the user clicks on btnCreate, a new <button> will be created beneath it. Clicking on any of the newly created buttons will not replicate the functionality. This is where you need to implement the live() method.

Live

<html xmlns="http://www.w3.org/1999/xhtml" >
<
head>
<
title></title>
<
script language="javascript" type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</
script>
<
script language="javascript" type="text/javascript">
$(function() {
$("input[type=button]").live("click", function() {
$(this).after("<br /><input type=\"button\" value=\"Child\" />");
});
});
</script>
</
head>
<
body>
<
form>
<
input id="btnCreate" type="button" value="Create Children" />
</
form>
</
body>
</
html>

In the code above, the user can now click on any of the buttons, and a new button will be created beneath it. The only difference is I’m using the live() method.

While choosing between bind() and live(), also note that we can bind only a single event in each call to the live() method whereas bind() can accept multiple events. Also blur, focus, mouseenter, mouseleave, change, submit are not supported with the live() method. Some more differences between live() and bind() have been listed over here



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET and regular presenter at conferences and user groups throughout Australia. Being an ASP.NET Insider, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with JavaScript. He also blogs regularly at DotNetCurry.com. Follow him on twitter @malcolmsheridan

comments

5 Responses to "When to use the jQuery live() and bind() methods"
  1. Randal L. Schwartz said...
    November 17, 2009 5:20 PM

    When Chuck Norris codes with jQuery, he doesn't use .live(), he uses .notyetkilledbyme().

  2. Wai Yan Maw said...
    December 24, 2009 8:02 AM

    But, it doesn't work properly when I use live() with anchor tag, that return false on click or another else. Anyway, we must do it (return false on click) so that the page doesn't scroll up to the top.

  3. rishi said...
    May 16, 2010 10:51 PM

    Could you Plz tell me what exactly the profit is, of using bind script?? does it make script faster???

  4. Suprotim Agarwal said...
    May 17, 2010 12:05 AM

    Rishi, if you read the article carefully, you will find that the advantage has been clearly summed up in one line "If you have controls that aren’t rendered dynamically, use bind(). If you have controls that are rendered dynamically, use live()."

  5. amskape said...
    November 23, 2010 8:35 PM

    Hi Friends ,
    FYI:live() support multiple events
    As of jQuery 1.4.1 .live() can accept multiple, space-separated events, similar to the functionality provided in .bind(). For example, we can "live bind" the mouseover and mouseout events at the same time like so:

    $('.hoverme').live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') {
    // do something on mouseover
    } else {
    // do something on mouseout
    }
    });

    Regards
    Anes P.A

 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions