Sherin
Sherin

Reputation: 431

get id of ul element using class name of li in jquery

I have the following html code

   <ul id="div0">
    <li id="INT" class="demographics">MemberID</li>
    <li id="VARCHAR" class="demographics">Name</li>
    </ul>
   <ul id="div1">
   <li id="INT" class="lab">DOB</li>
   <li id="INT" class="lab">ZipCode</li>
   </ul>
   <ul id="div2">
  <li id="VARCHAR" class="labresult">Payroll</li>
   <li id="INT" class="labresult">DrID</li>
  <ul>

in jquery i want to get the id of ul element using class name of li element,

for example

             $('.demographics').click(function(){
                 // here i want the id of ul tag containing class demographics
                  });

Upvotes: 0

Views: 2837

Answers (3)

Sven van de Scheur
Sven van de Scheur

Reputation: 1903

Just look for the parent() using jQuery: http://jsfiddle.net

$(this).parent().attr('id');

Upvotes: 2

Jai
Jai

Reputation: 74738

Try with this:

$('.demographics').on('click',function(){
    var id = $(this).closest('ul')[0].id;
    console.log(id);
});

Upvotes: 2

Hoja
Hoja

Reputation: 1207

Use this

$('.demographics').on('click',function(){
    var id = $(this).parent('ul').attr('id');
});

Upvotes: 5

Related Questions