titi
titi

Reputation: 1035

Get the element on the element when onclick on the sub element by jquery

I have these kind of list :

<ul id="navmenu-v">
  <li class="level1"><a id="56" class="s1">Accessories</a>
    <ul class="level2">
       <li><a id="232" class ='s2'>Apple</a></li>
       <li><a id="231" class ='s2'>Mango</a></li>
       <li><a id="154" class ='s2'>Orange</a></li>
    </ul>
  </li>
  <li class="level1"><a id="63" class="s1">Componet</a>
    <ul class="level2">
       <li><a id="129" class ='s2'>Leave</a></li>
       <li><a id="658" class ='s2'>Tree</a></li>
       <li><a id="158" class ='s2'>Roof</a></li>
    </ul>
  </li>
</ul>

How can I get the id of the class s1 such as 56 and 63, when I click on the class s2?

Thanks you.

Upvotes: 0

Views: 56

Answers (2)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76900

you could do

$('.s2').click(function(){
   alert($(this).closest('.level1').find('a.s1').attr('id'));
});

Upvotes: 2

Dogbert
Dogbert

Reputation: 222378

$(".s2").click(function() {
  console.log($(this).closest(".level1").find(".s1").attr("id"));
});

Sidenote:

id's cannot begin with numbers according to the html spec.

Upvotes: 4

Related Questions