What you want is a variant of the behavior pattern.
It allows you to automatically handle events for elements with specified classes or other attributes.
The usual implementation is to listen for events in the "document" and then, event.target, determine the action.
For example: fiddle ( http://jsfiddle.net/PRkAr/ )
$(document).on('click change', '.foo.bar', function(e) { var classes = this.className.split(/\s+/); $.each(classes, function(i, cls) { handle(e, this, cls); }) }) function handle(e, elem, cls) {
Here, the handle function handles all events on elements with the classes you have selected. If you want to add other events or classes, just add them to the "on" list.
Ilya Kantor
source share