How can I find the index of a clicked anchor tag from an array with jquery???
I want to search if there is an element that eqqal to clicked element, and if is true return the index of that element.
i tried with something like this but it return 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 can you help me??!
I'm not sure what all the get
and makeArray
stuff is for but I think you're looking for index
:
Search for a given element from among the matched elements.
So given some anchors:
<a>Zero</a>
<a>One</a>
<a>Two</a>
<a>Three</a>
<a>Four</a>
you could do things like this:
$('a').click(function() {
var i = $('a').index(this);
// i is the index of the clicked anchor within all the anchors.
});
How about this:
$('a').click(function(){
console.log($(this).index());
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.