Determining index of clicked element inside UL - Javascript jQuery

Javascript examples for jQuery:Mouse Event

Description

Determining index of clicked element inside UL

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.js"></script> 
  <script type="text/javascript">
    $(function(){//from www.  j  a v a 2 s  .  c  o m
$("ul li").click(function() {
    console.log("You're clicked " + ($(this).index() + 1) + "/" + $("li", $(this).parent()).length)
});
    });

      </script> 
 </head> 
 <body> 
  <ul> 
   <li>1</li> 
   <li>2</li> 
   <li>3</li> 
   <li>4</li> 
  </ul>  
 </body>
</html>

Related Tutorials