How to find the index of a clicked element from an array with jquery?

How to find the binding tag index of a binding from an array using jquery ???

I want to search if there is an element that is eqqal for the clicked element, and if is true returns the index of this element.

I tried with something like this, but it came back with -1

$('#id').click(function(){ var obj = $('a').get(0).href; var arr = $.makeArray(obj); var getclickedhref = $(this).get(0).href; var clickedindex = $.inArray(getclickedhref, arr); console.log(clickedindex); }); 

Please help me!!

+4
source share
2 answers

I'm not sure if all things get and makeArray , but I think you are looking for index :

Find the given element from among the matched elements.

So, let's set some bindings:

 <a>Zero</a> <a>One</a> <a>Two</a> <a>Three</a> <a>Four</a> 

you can do things like this:

 $('a').click(function() { var i = $('a').index(this); // i is the index of the clicked anchor within all the anchors. }); 

Demo: http://jsfiddle.net/ambiguous/YbUU7/

+10
source

How about this:

 $('a').click(function(){ console.log($(this).index()); }) 
+2
source

Source: https://habr.com/ru/post/1394811/


All Articles