Search code examples
javascriptjqueryvariablescss-selectorstraversal

How to write a selector like this: $('.classname.active') if '.classname' is stored in a $var?


I want to store a selector in a variable and I want to use that object in instances where I need to find a class of '.active'. Which would return the object that is '.active'.

Basically, how would i write a selector like this: $('.classname.active')

var $varName = $('.multipleDivs');
$varName.hasClass('active'); // this returns true, but not the object

var $varName = $('.multipleDivs'); 
$('.active', $varName); // this returns an empty array

Solution

  • Your original guess is the right way to do it.

    $(".multipleDivs.active")
    

    alternatively, you can do

    $varName.filter(".active")