jQuery .delegate()…and what it does

So the whole point of my messing around with the functions page, was to change the call to the google jQuery file, to 1.4.2 instead of 1.4.1. The latest version supports .delegate(), which I wanted to see how to use it.

I’ll give you a sample of the jQuery I am using for my slider:

$('#controlList li').click(function(evt) {

This is the section that controls the list in the slider, which moves the slides left and right. From what I have read, which has been quite a bit now, and COMPLETELY confusing until I read this post, the way I had it above (idiot terms here)…it attaches the click handler to each li when it is clicked.

This is what I have now:

$('#controlList').delegate("li", "click", function(evt) {

The click event is actually attached to #controlList, which then checks to see if an li within it was clicked, if so, it runs the function. Meaning only one handler on the #controlList because of .delegate();, instead of a handler on each li. Way better preformance and less waste on jQuery’s part.

And of course, because the Reference Guide I bought was written before 1.4.2 (only 1.4), delegate() is not covered. Bastards!

Tags: ,

2 Responses to “jQuery .delegate()…and what it does”

  1. Sweet snippet! Thanks, Jeremy.

    • jcDesigns says:

      Wow this is an older post. I assume you got the link from the one I wrote the other day. Glad you like!

Leave a Reply