jQuery “live” event

jQuery 1.3 has a brand new and powerful event. It was very painful and a nightmare to maintain events for newly created elements. For example if you define “click” event for existing elements at domready it won’t work for dynamically added elements later. Each time a new element is added you had to bind the event to that element. Which results in many recursive function calls, for huge applications so tough to maintain the events dynamically and finally not efficient coding structure.

The new “live” event has made our life easier to a great extent!

For example:

$("a").bind("click", function(){$(this).blur();});

  This simple code will remove the unwanted selection for your EXISTING “a” tags only. But if any new “a” tag is added dynamically to the dom, you have to do the same thing again. But if you use

$("a").live("click", function(){$(this).blur();});

  this will remove your headache about new “a” tags which will be added later.
So live event will reduce the load of calling same function(for applying same event) again and again for new elements. To unbind live event you have to use “die”.

One very important point to note – live event only works for direct selectores. For example, this would work:

$("li a").live(...)

but this would not:

$("a", someElement).live(...)

and neither would this:

$("a").parent().live(...)

I must say jQuery is like a “Lamp of Aladdin” for web developers which has made javascript application development a fun. And the new live event will boost up that fun and make more time for dating ;)

Documentation: http://docs.jquery.com/Events/live

Related posts:

  1. A better process to find maximum z-index within a page
  2. jQuery vs. prototype.js
  3. Advanced cycle plugin integration
  4. IE6 fix for select box over absolute positioned element
  5. Gmail uploader clone with drag drop feature : Free Download with source code

6 Comments to “jQuery “live” event”

  1. Hellen CLARK 26 March 2009 at 9:43 pm #

    Hey, I justed wanted to give you a compliment on your blog, keep up the great work. I will be back to check it out in the near future.

  2. adnan 16 June 2009 at 3:41 pm #

    Thank you Hellen

  3. fartman 13 October 2009 at 3:35 am #

    Your blog sucks.

  4. adnan 17 October 2009 at 7:30 am #

    thanks @fartman!

  5. ingerh 13 January 2010 at 8:36 pm #

    whats your problem fartman ??? this is a great site ill be back…

  6. Jay 24 January 2010 at 10:28 am #

    @fartman: You mum sucks. But she was cheap ;-)


Leave a Reply