The question for today is how to bind a single function to multiple different elements (in other words, elements with either different classes or IDs) in a single call.
Well actually, this is remarkably simple, but only because I JUST discovered that since day 1, jQuery supports the concept of a multiple selector!
Simply put, you can specify as many selectors as you want to in the standard format, though you are delimiting them with a comma, and jQuery will then combine the results returned by the multiple selections into a single result set.
(Note: another way to achieve this multiple selection is by using the combinator .add() method)
An example of this in the wild could be:
$('#message-ajax,.error-notice,.success-notice').click(function(){ $(this).hide(); });
Nifty. You learn something new every day! :)
Related Link: http://api.jquery.com/multiple-selector/
You might also enjoy:
-
Sometimes it is nice to present a multiple select listbox to a user with everything already selected. Handy if you're using listboxes as filter controls, ...
-
Multi-select boxes are wonderful creatures in that they provide a particularly easy to implement user interface element that provides a great deal of functi ...
-
Grabbing all the selected values or even text values from a multiple select listbox turns out to be quite simple if you know which tools to use. The idea ...
-
Four years have passed since the initial public release of the stellar jQuery library and in celebration the guys over at jQuery have just released jQuery 1 ...
-
The well-established javascript library jQuery comes bundled with a neat little function that handles loading remote content directly into the current page' ...