Jquery - More Efficient To Use Multiple Selectors, Or Each
I have something like this ... .. . $('.Class1').so
Solution 1:
As far as fastest selectors go it would be better to do:
<div id="container">
<input data-something="1" />
<input data-something="2" />
<input data-something="3" />
<input data-something="4" />
</div>
Then you can do:
$('#container input').each(function(){});
Solution 2:
Irrespective of how you select them, you'll need to do an each()
anyway if your code example is anything like you're real code.This is because you're passing unique data to each function call.
But you should add a tagName
to the selector at the very least.
$("input[something]").each(function() {
$(this).someFunction($(this).attr("something"));
});
Without the tagName
, jQuery will need to look at every element on the page, instead of just input
elements.
Solution 3:
^=
operator matches starts with too:
$("input[class^=Class]").each($(this).someFunction($(this).attr("something")));
Post a Comment for "Jquery - More Efficient To Use Multiple Selectors, Or Each"