Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the id of the class which has class active?

Tags:

jquery

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')
like image 991
PythonEnthusiast Avatar asked Dec 21 '13 14:12

PythonEnthusiast


People also ask

How do you find the class ID?

The class ID is a unique number that is automatically generated upon the creation of a class. You can find this on your instructor homepage, under the column 'Class ID', right next to the class name.

How check class is active or not in jQuery?

The jQuery hasClass() method is used to check whether selected elements have specified class name or not. It returns TRUE if the specified class is present in any of the selected elements otherwise it returns FALSE. Syntax: $(selector).

How do I select an item using class or ID?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.


1 Answers

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

like image 146
Arun P Johny Avatar answered Sep 30 '22 16:09

Arun P Johny