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??!
The buttonPressed() callback function will have a returned event object which has all the data about the HTML element that is clicked on. To get the clicked element, use target property on the event object. Use the id property on the event. target object to get an ID of the clicked element.
index($(this)) to get the index of the button inside the table.
slice() method constructs a new jQuery object containing a subset of the elements specified by the start and, optionally, end argument. The supplied start index identifies the position of one of the elements in the set; if end is omitted, all elements after this one will be included in the result.
To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”). click() method. The user can attach a function to a click method whenever an event of a click occurs to run the function.
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.
});
Demo: http://jsfiddle.net/ambiguous/YbUU7/
How about this:
$('a').click(function(){
console.log($(this).index());
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With