Home > Uncategorized > Finding n-th level of sibling on DOM with jquery

Finding n-th level of sibling on DOM with jquery

By clicking on an element in DOM, to get the which indexed number of the sibling, you’ll need to use jquery’s .index() method which is easy and useful too.
Let’s give an example,
HTML :

 <ul>
     <li>first</li>
     <li>second</li>
     <li>third</li>
 </ul>

Javascript:

$('li').click(function () {
    alert($(this).index());
});

Enjoy jquery

Categories: Uncategorized Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Spam protection by WP Captcha-Free