Of course I have a backup!

Random blobs of wisdom about software development

Common JavaScript patterns: Event delegation

Monday, October 31, 2011

I see these types of questions a lot on forums:

I have made an ajax based favorites system on my webshop. Users can click on the "add" button next to each product, and it will be added to their favorites list. Everything works fine, except that on freshly favorited elements, the "remove" button does not work, until I refresh the page. What could cause this? Here is the code. I'm using jQuery.

Without even taking a look at the code, I already know what the problem is. The problem usually has nothing to do with jQuery, but given the popularity of it, but it usually gets associated to it. The question can come in various flavors, but the pattern is always the same:

There is a container element, that has elements added to it via DOM manipulation, and the newly added elements behave in unexpected ways, usually some functionality is not working, until a page refresh is made.

(Continued)

Tagged as: | Comments (0)