Last week Nettuts’ editor Jeffrey posted about a new screencast competition. This isn’t my entry – it’s just a test-runner to see if I’m any good at the whole screencast thing. It’s a 30-minute screencast about event delegation in jQuery and in it I explain event bubbling/propagation and then go on to demonstrate how to develop an event delegation plugin for jQuery.
Note: The quality isn’t too great but if you make it full screen it’s much better!
Thanks for watching, if you have any feedback I’d love to hear it! I’m planning on recording my entry for that competition soon, it’ll also be on jQuery although I’m not sure of the exact topic yet…
Here’s the plugin we created in the screencast:
$.fn.delegate = function(selector, event, action) { return this[event.replace(/^on/,'')](function(e){ var target = e ? e.target : window.event.srcElement, elements = $(selector, this), elementsLength = elements.length; elementLoop: while (elementsLength--) { var parents = $(target).parents(), parentsLength = parents.length; parentLoop: for (var pi = 0; pi < parentsLength; pi++) { if ( $(parents[pi]).is(selector) ) { return action.call(elements[elementsLength]); } } if (elements[elementsLength] === target) { return action.call(elements[elementsLength]); } } return true; // We don't want to prevent default action so returning true is best }); } // USAGE EXAMPLE: $('ul').delegate('li', 'click', function(){ $(this).parent().append( $(this).clone() ); }); |
Thanks for reading! Please share your thoughts with me on Twitter. Have a great day!
This is the first screencast I’ve seen that covers this at all. Brilliant.
Well done!
Many “um”s, James. In future try to suppress those and know that, in post production, you usually can edit most of these out.
Thanks for the feedback!
@Steven, 😀 Yeh I know, I thought about editing them out but didn’t think it was worth the time, plus the software I’m using is a bit limited in that respect. Thanks for the advice Steven!
LOL, just flicking through the video, I do say “UM” and “AH” A LOT!! Sorry about that! I do get a bit better as the video progresses though.
This was fantastic. Over the weekend, I wrote an event delegation version of an ‘in place editor’. I had my own version of parentLoop, but I didn’t know about the ‘is’ method [$(parents[pi]).is(selector)] you used. I’ll definitely incorporate this snippet of code into mine before posting up my editor.
Thanks!
People forget how tough it is to record a screencast and write code at the same time. When you’re focusing on so much, it’s easy to add the extra “umms”. I do it too.
Have you tried out the free 30 day trial of Camtasia? It’s the best I’ve found.
Very impressive for a first screencast. Like Jeffrey said its tough to code & record at the same time. You really did a good great job.
Hey James,
Thanks for the screencast. There’s actually a more elegant way to check if an element is a specific type of node; instead of:
use:
Hey great job, better than I would have done! And I must say its good to hear a fellow brit on this place called the interweb design community 🙂
Thanks for all the comments!
@Jerry, glad I could help 🙂
@Jeffrey, I’m actually using Camtasia already; it’s okay although I wouldn’t mind a little more control in the “producer” module. I considered exporting the video out to Adobe premiere but when I tried the quality became awful, even when I used the Techsmith codec. The reason I wanted to use Premiere for post-production was because it offers noise-reduction and some cooler transitions. I guess I’m stuck with “Camtastia producer”…
@Rob, @Simon, thanks for your kind comments!
@Nachocab, thanks. 🙂 I forgot about that at the beginning of the screencast but I did use it in the resulting plugin.
Great screencast James! We need more on event delegation and how to use it properly.
The stuff you do with the event object for IE isn’t necessary as far as I can find out. According to the jQuery web site the event object that gets passed is standardized in all browsers.
Just writing e.target.nodeName for example works in IE as well without first running the conditional statement you set up.
The screencast is great James. You should be proud of yourself – the content of the screencast is spot-on, and the odd ‘um’ and ‘ah’ doesn’t take anything away from the technical expertise on show.
I also agree with Rob of course – big up to the Brits! 🙂
@James – Try exporting it as an AVI, 640×480, 5 fps, and then upload it to Blip. They do a really good job with the converting…almost WYSIWYG.
great job
now i know that this is an example
but jquery’s clone method provide event handler duplication if you pass true to clone
$().clone(true);
i enjoy reading your posts!!!
keep up the great work
You mentioned an extension for firebug y something can you give me the name again so I can find it.
Sorry about this late reply!
@Shane, thanks mate, I’m glad you liked it! 🙂
@Jeffrey, cool, thank you for the advice. Btw, I’m currently recording my SC for the NETTUTS competition, should be in by the 31st!
@Eugene, lol, yeh I guess I could have chosen a better example! I completely forgot about that feature when recording it!
@Steve, it’s called Widerbug, it does take some getting used to but it definitely makes using firebug easier on a bigger screen!
Brilliant tutorial and thanks for the tip on Widerbug. I installed it now.
Thanks alot. Just starting out with jQuery, and you explained event delegation in a very clear and precise manor. For a first screencast, it was really good. Keep up the good work.
great screencast. very informative. All the tutorials on event delegation center around the click() event. I’m trying to get around a performance problem in simply changing the class of a “tr” element upon hover. I am fairly novice and cannot seem to manage the event target and how to delegate the mouse events from the parent table. the problem I’m having is that although in firefox, the page loads quickly, in I.E., there is a lag as all the “tr” elements are bound with the hover event.
Great stuff James, I just started learning event delegation and now I’m much more wiser!