I came to the conclusion that dynamic event handlers can make my life much more easier, so I can write more cleaner and maintainable code. My idea was to add different class names to an element to bind different event handlers to it. This way, I could get rid of some global variables and switch case loops that i needed to manage my functions. The second advantage is, that you can chain event handlers to do things one after another.

Here is a small example that adds classnames to a button and binds dynamic event handlers to the class attribute. Depending on the class attribute an event handler fires an alert box that indicates which classname was added to the button.

weiterlesen