jQuery HTML Element How to - Get UL item index








Question

We would like to know how to get UL item index.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from  w  ww . j  a  v a 2  s.c om-->
   console.log( $('ul li.active').index() + 1 );
});
</script>
</head>
<body>
  <ul>
    <li>1</li>
    <li class="active">2</li>
    <li>3</li>
    <li>4</li>
    <ul>
</body>
</html>

The code above is rendered as follows: