Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the index of list Items on click of li using jquery

Is there a way through which I can get the index of list Items on click of li element using JavaScript/jQuery?

<ul>
  <li>item1</li>
  <li>item2</li>
  <li>item3</li>
</ul>
like image 901
Bin2k Avatar asked Sep 28 '10 09:09

Bin2k


People also ask

What is $$ in jQuery?

$ is another, which is just an alias to jQuery . $$ is not provided by jQuery. It's provided by other libraries, such as Mootools or Prototype.

How to get the index of an element in jQuery?

jQuery Misc index() Method The index() method returns the index position of specified elements relative to other specified elements. The elements can be specified by jQuery selectors, or a DOM element. Note: If the element is not found, index() will return -1.

How to give index in jQuery?

The index() is an inbuilt method in jQuery which is used to return the index of the a specified elements with respect to selector. Parameter: It accepts an optional parameter “element” which is used to get the position of the element. Return value: It returns an integer denoting the index of the specified element.

Which function is used to get index of a string in jQuery?

The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.


1 Answers

$('ul li').click(function(){ alert($(this).index()); });
like image 99
Thomas Clayson Avatar answered Nov 15 '22 08:11

Thomas Clayson