Praful Bagai
Praful Bagai

Reputation: 17372

get the id of the class which has class active?

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

Answers (4)

Muhammad Rashid
Muhammad Rashid

Reputation: 583

Try this , #configuration_sidebar_content li.a.active change to $("#configuration_sidebar_content li.active").attr('id')

http://jsfiddle.net/yWsRR/

Upvotes: 0

Anto Subash
Anto Subash

Reputation: 3215

you can also try

$("#configuration_sidebar_content li").find("a.active").attr('id')

Upvotes: 2

Michał Rybak
Michał Rybak

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

Arun P Johny
Arun P Johny

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

Related Questions