Thursday, 22 August 2013

Add callback to the class or individual elements

Add callback to the class or individual elements

I use JQuery, and I have a collection of buttons all of which have class
btnClass. Some (or all) of them are toggled visible/invisible repeatedly
based on some changing condition.
I want to add a callback to the click event when any of these buttons are
clicked. I have two options:
Add a callback to all of them statically and upfront using the class
selector: $(".btnClass").on("click", function(e) { ... });
Or add a callback dynamically and individually when a button is toggled
visible: btn.toggle(true).on("click", function(e) { ... });
Performance wise, which is better and why? Is there some other approach
that is better than both?

No comments:

Post a Comment