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)
Another way to make objects in Javascript using JQuery , getting data from the dom and pass it to the object Box and, for example, store them in an array of Boxes, could be: var box = {}; // my object var boxes = []; // my array $('div. test'). each(function (index, value) { color = $('p', this).
Introduction to jQuery object. The jQuery object is a collection of DOM elements and behaves like a special array. Everything in jQuery is an object.
The jQuery selector finds particular DOM element(s) and wraps them with jQuery object. For example, document. getElementById() in the JavaScript will return DOM object whereas $('#id') will return jQuery object.
$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.
Assuming you have raw DOM elements (not jQuery objects such as $('#abc')
or $('.abc')
), there's no need to use add
:
$([A,B,C,D]).show();
See the documentation of the $(...)
function for details.
If you really want something like $(A,B,C,D)
where A,B,C,D
are jQuery objects, then the only real way is to roll your own function.
This does the trick:
var all = function() {
var objs = $();
$.each(arguments, function(i,e) {
objs = objs.add(e);
});
return objs;
};
// ...
all(A,B,C,D).show();
Working example: http://jsfiddle.net/Jb6VD/5/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With