jQuery caching of selectors

javascript, jquery

Solution

You can use .add()

$selector1.add($selector2).add($selector3).addClass('myClass');

Problem

So I know if I am using a selector more than once its better to cache it in a javascript variable. What if I want to perform the same action on multiple jQuery selectors that are stored in variables? ex ``` var $selector1 = $('#div1'); var $selector2 = $('#div2'); var $selector3 = $('#div3'); //do some work here on each individual div //now I want to do this $('#div1, #div2, #div3').addClass('myClass'); ``` Is there a way to do that on the 3 variables? (something like `($selector1, $selector2, $selector3).addClass('myClass');`)

Original source