Let’s suppose I have a function that is passed multiple jQuery objects.
function manipulateStuff(obj1, obj2) { /* ... */ }
manipulateStuff($("div"), $("h1"));
Now let’s suppose I want to perform some actions on both of them.
I can always repeat the code for each, or write some code that loops through them, but I don’t feel like doing that. I want some way of combining the elements of multiple jQuery objects. The following code doesn’t work, but it demonstrates the sort of thing I’m looking for
$(obj1, obj2).css("color", "red");
Is there already a way to do this built in to jQuery? If not, can I write a small plug-in to do it?
EDIT: to be clear, I’m manipulating jQuery objects, not string selectors. I am already aware of the comma operator in selectors.
The easiest way if you know the selectors is to just use a comma:
If you have anonymous jQuery objects and want to merge them, you can create a simple utility function that merges several jQuery objects of elements:
http://jsfiddle.net/hvm7z/