Binding events is one of the most common tasks a JavaScript developer has to do. It's integral to the way our applications handle user interaction and response. Since it's inception, jQuery has had event handling via various helper methods that helped to abstract cross-browser differences. If you're a jQuery developer, I'm sure you're used to syntax like this:

[code lang="js"]
$(&quot;#clname&quot;).click( function() { alert( &quot;Rey&quot; ) } );
[/code]

which would bind a click event to a DOM element with an ID of "clname". Very easy to understanding and simple to use. But when you get past just simple use of JavaScript and start inserting new DOM nodes, this binding becomes less useful because it can't handle dynamically inserted elements. Let me explain.

Say you have the following HTML markup:

[code lang="html"]
&lt;div id=&quot;clcontainer&quot;&gt;
&lt;a href=&quot;#&quot; class=&quot;clients&quot;&gt;Click for Rey Information&lt;/a&gt;
&lt;/div&gt;
[/code]

and you bind the click event to generate an alert of some type like this:

[code lang="js"]
$(&quot;.clients&quot;).click( function() { alert( &quot;Here's some information.&quot; ) } );
[/code]

but then you decide that you're going to add a new DOM element to the page dynamically:

[code lang="js"]
$(&quot;#clcontainer&quot; ).append( &quot;&lt;a href='#' class='clients'&gt;Click for Mark Information&lt;/a&gt;&quot; )
[/code]

What do you think will happen? I would venture many developers might expect that the second, dynamically appended DOM element would share the same binding as the first since they share the same class name. That's not the case. jQuery methods such as click(), dbclick(), and bind() are meant to bind events to a single DOM element.

Now to get around this limitation, jQuery team member <a href="http://brandonaaron.net/">Brandon Aaron</a> initially created a plugin called LiveQuery which would allow you to bind events not only to specific DOM elements but all subsequent DOM elements that were appended to the DOM that matched the selector specified in the initial binding. That plugin eventually became part of the jQuery Core library and renamed simply to <a href="http://api.jquery.com/live/">live()</a>. The great thing with <a href="http://api.jquery.com/live/">live()</a> is that you could now bind dynamically added DOM elements like this:

[code lang="js"]
$(&quot;.clients&quot;).live( &quot;click&quot;, function() { alert( &quot;Here's some information.&quot; ) } );
[/code]

and if you did append a new DOM element, like this:

[code lang="js"]
$(&quot;#clcontainer&quot; ).append( &quot;&lt;a href='#' class='clients'&gt;Click for Mark Information&lt;/a&gt;&quot; )
[/code]

it would now share the same function binding as initially defined.

<strong>The Problem with Live()</strong>

Now, live() is an awesome method and people totally loved it. That is, until they wanted to bind events based on a deeper DOM traversal than just a single element, specifically when methods were used that altered the selector expressions initial results (e.g.: using children()). So, if we had markup like this:

[code lang="html"]
&lt;div id=&quot;clcontainer&quot;&gt;
	&lt;ul&gt;
		&lt;li&gt;Mary&lt;/li&gt;
		&lt;li&gt;Jane&lt;/li&gt;
	&lt;/ul&gt;
&lt;/div&gt;
[/code]

and tried to use live() to bind all of the list items like this:

[code lang="js"]
$(&quot;#clcontainer&quot;).children( &quot;ul&quot; ).find( &quot;li&quot; ).live( &quot;click&quot;, function() { alert( &quot;Here's some information.&quot; ) } );
[/code]

the binding would fail. 

In order to get around this, Brandon introduced in version 1.4 of jQuery the new <a href="http://api.jquery.com/delegate/">delegate()</a> method. It provides greater control by allowing you to specify they context to which you'd like to bind to. So using the same example as above and making some slight changes to use delegate(), we're now able to use chaining to determine our selector results and then specify that we'd like all current and future list items to be bound to our declared function:

[code lang="js"]
$(&quot;#clcontainer&quot;).children( &quot;ul&quot; ).delegate( &quot;li&quot;, &quot;click&quot;, function() { alert( &quot;Here's some information.&quot; ) } );	
[/code]

<strong>When to Use live() or delegate()</strong>

These two convenience methods are totally awesome and incredibly helpful with more complex apps. The best use case for them is when you know that you will be dynamically adding new DOM elements and they'll share the same bound function. I mean, essentially that's the premise of event delegation. You're trying to limit the number of event handlers needed to handle functionality and increase maintainability be centralizing your logic. Make sense. If you're not going to be doing anything that involved, though, then jQuery's event helper methods such as bind(), click() etc. are still excellent choices for those one-off scenarios.


