Yeah, the way I was doing is
A.add(B).add(C).add(D).show()
While A,B,C,D are jQuery objects. I wonder if there’s such a simpler way to do this out there?
I’ve tried all the following approaches, but no results:
$(A,B,C,D).show()
A.add(B,C,D).show()
All suggestions are welcome!
Addition to clarify the question:
The part “.show()” is just for demonstration. I just wanted to know how could I create a set of JQuery object like $(‘p’) create a set of p tag.
In my real case, I used
$([A,B,C,D]).each(fn)
instead of .show() (And I wonder why this worked?)
It’s obviously that
$([A,B,C,D]).each(fn)
$('p').each(fn)
both work. But
$([A,B,C,D]).show() //--doesn't work
$('p').show() //--works
Just the second line works. Does anyone know the diffrence between them ?
(I just thought they’re the same, then made a bit of tangle in my question)
Instead of using a selector this solution uses a jQuery method called $.each which takes an array and iterates over it. The array passed is a set of jQuery objects.
$(this)referes to the jQuery objects being iterated on each time.demo: http://jsfiddle.net/SCjMc/1/
Other facts about how $() works:
$(element)is an shortcut forjQuery(element). ThejQuery()method accepts a different set of parameters:The description for each type of parameter can be found in this link.
One of the type of parameters is “elementArray”. The description for this is:
The catch is that when you use jQuery to select an element this returns a jQuery object and not a DOM Element directly. Therefore this will not return any elements:
To return a DOM Element instead of a jQuery object you have to access the property in the position of
0of the jQuery object. As follows:$("element")[0]. And that is why this will work: