JQuery dynamic event handler

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.

The JQuery „.live()“ function i no real event handler binding, but it works like charm for my idea.

Here is the code example

Here is the result

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.