Saturday, October 31, 2009

Javascript - Event Delegation

As soon as I started getting into JavaScript, I started hearing about event delegation.  I think every book on JavaScript I have read has at least mentioned event delegation.  I think maybe I understood a little bit, but didn't really grasp it.  Until now.  I'm not sure what was different about the way it was explained, but in Cody Lindley's book, "jQuery Enlightenment", the explanation of event delegation just clicked for me.  I've known that in JavaScript when you are adding new elements that have events bound to them, you need to keep binding the events for every new element you add.  Another newer option is to use the jQuery function "live()", which will watch everything in your selector for a certain event.  Or, I knew that event delegation could solve this problem, but I didn't quite get how to make it work for me, until now.

I don't want to copy and paste Cody's exact code in this post but I think it's okay to post the link to JS Bin where the code snippit lives.  The purpose of this blog is to help me remember stuff I want to remember, so having this link will help me later on when I come back to this post.

The link to the JS Bin snippit is located here.

I am learning so much from this book, "jQuery Enlightenment".  I thought I knew a bit about jQuery, but this book has helped me learn so much more.  I highly recommend you go to the site and buy it.  Thanks Cody!

No comments:

Post a Comment