---
title: "jQuery edge delegate method has arrived"
description:
  "jQuery delegate method can be used instead of live. In many cases it is
  desirable to use delegate over live. This blog discusses how delegate works
  and why it is preferred over live."
canonical_url: "https://www.bigbinary.com/blog/jquery-edge-delegate-method-has-arrived"
markdown_url: "https://www.bigbinary.com/blog/jquery-edge-delegate-method-has-arrived.md"
---

# jQuery edge delegate method has arrived

jQuery delegate method can be used instead of live. In many cases it is
desirable to use delegate over live. This blog discusses how delegate works and
why it is preferred over live.

- Author: Neeraj Singh
- Published: February 2, 2010
- Categories: jQuery

One of the issues with live method is that live method first searches through
all the elements then throws away the result.

```javascript
$('p').live('click', function({})
```

In the above case jQuery does nothing with the selected `p` elements. Since the
result does not really matter, it is a good idea to remove such codes from the
`document ready` callbacks list.

So instead of doing this

```javascript
$(function(){
  $('p').live('click', function({})
})
```

just do this

```javascript
$('p').live('click', function({})
```

## Going a step further

John just landed
[this commit which adds delegate](http://github.com/jquery/jquery/commit/31432e048f879b93ffa44c39d6f5989ab2620bd8#comments)
method .

Html markup

```html
<div id="lab">
  <p>p inside lab</p>
</div>
<p>p outside lab</p>
```

If you want to track all the clicks on p then you could write like this.

```javascript
$(document).delegate("p", "click", function () {
  log("p was clicked");
});
```

However if you only want to track clicks on 'p' which are inside the id `lab`
then you can write like this.

```javascript
$("#lab").delegate("p", "click", function () {
  log("p was clicked");
});
```

Note this functionality is in jQuery edge and is not available in jQuery 1.4.1.
So you will have to get jQuery code from github to play with it.

If you are interested in the jQuery.live vs jQuery.fn.live discussion then
follow
[this thread](http://forum.jquery.com/topic/jquery-live-jquery-fn-live-discussion)
.

## Links

- [Human page](https://www.bigbinary.com/blog/jquery-edge-delegate-method-has-arrived)
