I found similar questions while searching for a solution for my problem but I don’t think its quite that what I’m looking for.
Using jQuery’s .on() is no brainer for binding events on dynamically created elements, but I got stuck while trying to pass different parameters to call different functions. After reading https://developer.mozilla.org/en/JavaScript/Guide/Closures I came up with the following working example, but I would like to know if its the best way to do it or if there is a more elegant solution?
Plus I often read that using eval() isn’t a best practice due to vulnerability issues but is there another way to build dynamic function names?
Edit:
Thanks a lot for everyone for the great answers! Did one of the answers got deleted though? I liked that one because of the hint to store the parameter in an attribute. Thanks again all!
The point of event delegation is that you don’t need to bind an event to every single element (or to the same element multiple times), just once to the common parent, like so:
To avoid using
eval, you could have an array of functions and call them by index:http://jsfiddle.net/V25Zc/2/