Suppose I get a list of items like $(".box"). Is it possible to get an indexed jQuery object
like
var $boxes = $(".box"),
$box2 = $boxes[1]
currently I do something like
var $boxes = $(".box");
$boxes.each(function(i, box) {
var $box = $(box); // <-- is this a good idea?
// do something with $box
});
I wonder tho if the line var $box = $(box) is such a good idea? I am actually running that in a setInterval()
like
var $boxes = $(".box");
setInterval(function() {
$boxes.each(function(i, box) {
var $box = $(box); // <-- is this a good idea?
// do something with $box
});
}, 1000);
I wonder if its bad for performance since I am initializing a variable for each item in $boxes per 1s in this example. If I can access the element directly from the jQuery “array” or whatever $boxes is, it maybe better?
You can iterate though the jQuery elements as per your example, nothing wrong with that. Creating a local variable for each element
var $box = $(box);is a good idea.You can also access the elements of the jQuery object with the eq method, e.g:
That way you don’t need to pass the element through the $ constructor.