For my project I’m using cached selectors to speed up, and see improvements:
(to reduce searches inside the document)
var sel1 = $('#selector1');
var sel2 = $('#selector2');
how can I use cached selectors in this situation? for ex:
$('#selector1, #selector2').fadeTo(300, 1, 'linear');
It’s just to polish up my code
Ty 🙂
You can use
.add()to “Add elements to the set of matched elements”:Docs for
.add(): http://api.jquery.com/add.add()can take in:$('<selector>', <context>))You can also pass an array of DOM elements to jQuery:
Here is a demo: http://jsfiddle.net/3xJzE/
UPDATE
I created a jsperf of the three different methods that are currently answers: http://jsperf.com/jquery-fadeto-once-vs-twice (it seems like using an array selector is the fastest:
$([one, two]).fadeTo...)