Reputation: 2683
Example HTML:
<ul class="nav">
<li class="item">Item 1</li>
<li class="item">Item 2</li>
<li class="item active">Item 3</li>
<li class="item">Item 4</li>
<li class="item">Item 5</li>
</ul>
Now I want to get active element inside of .nav
:
function someFunction() {
var elements = $('.nav').find('.item');
var active = elements.get('.active');
}
Of course that .get
will not work. But this is example what I want to get. So I have variable containing elements and I want to get that .active
element. I don't want $('.nav').find('.item.active')
, because this is just an example, my code is much more complex and I want to work with that variable. Is it even possible in jQuery?
Upvotes: 1
Views: 58
Reputation: 347
You can access the variable as jQuery element.
var elements = $('.nav').find('.item');
$(elements).filter('.active');
Upvotes: 2
Reputation: 1234
You can select the active li inside ul directly by
var element = $('.nav li.active')
Upvotes: 0
Reputation: 15509
are you after the text of the active item? then target it directly
var active = $('.nav .active').text();
if you want to display only the active item then:
$('.nav .item').css('display','none');
$('.nav .active').css('display','block');
Upvotes: 1
Reputation: 5039
Use .find()
method to get the active class element inside entire ul.
var activeObj = $(".nav").find(".active");
Upvotes: 0