Reputation: 17372
I want to get the id of the id of the element which has class active. I've viewed this thread , but isnt able to get the o/p I want.
<ul class="nav nav-pills nav-stacked navigation" id="configuration_sidebar_content">
<li>
<a>Location</a>
<ul class="nav nav-pills nav-stacked " id="location" style="padding-left:30px;">
<li class="active" id="region"><a>Region</a></li>
<li id="country"><a>Country</a></li>
</ul>
</li>
<li>
<a>Device</a>
<ul class="nav nav-pills nav-stacked" style="padding-left:30px;">
<li id="gateway_unit"><a>Gateway Unit</a></li>
<li id="meter_unit"><a>Meter Unit</a></li>
</ul>
</li>
</ul>
I tried this.
selected_item_id = $("#configuration_sidebar_content li.a.active").attr('id')
Upvotes: 6
Views: 33725
Reputation: 583
Try this , #configuration_sidebar_content li.a.active
change to $("#configuration_sidebar_content li.active").attr('id')
Upvotes: 0
Reputation: 3215
you can also try
$("#configuration_sidebar_content li").find("a.active").attr('id')
Upvotes: 2
Reputation: 8706
You code didn't work, because you wrote .a
, which means having class a
, which wasn't what you're looking for.
Try this:
$("#configuration_sidebar_content").find(".active").attr('id')
Using find()
is more effective than selectors with spaces inside (e.g. li a
).
Upvotes: 3
Reputation: 388316
it should be
$("#configuration_sidebar_content li.active").attr('id')
there is no class a
assigned to the li
element, a
is a child element of the li
element
Upvotes: 15