How to get the name of the last parent div with parents?

2

I have a sequence of divs like the ones below.

<div class="upage" id="id01">
 <div class="main" id="id02">
   <div class="main2" id="id03">
   </div>   
 </div>
</div>

I need to get the id of the class that is above in this case should return id01 . I use parents(".upage"); but it comes to me with a pointer to the whole div. I just need to get the ID.

How to do it?

    
asked by anonymous 14.10.2015 / 22:59

1 answer

3

I suggest you use .closest() that receives an argument that can be a class. Then the attr('id') that fetches the element ID that .closest() returned.

$('input').on('focus', function(){
    var id = $(this).closest('.upage').attr('id');
    // fazer algo com o id
});

Example: link

    
14.10.2015 / 23:08