Avatar of hankknight
hankknight
Flag for Canada asked on

jQuery: Get index number of matching link

Using jQuery, how can I find the index number of a link clicked on with a matching class name?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
 $('a.xyz').click(function(index) {
  alert('You clicked on link number ??? with the class name of "xyz".');
  return false;
 });
});

</script>
</head>
<body>

<h1>Click on a link below.</h1>
<p>
 <a href="#" class="xyz">Link</a>
 <a href="#" class="xyz">Link</a>
 <a href="#" class="xyz">Link</a>
 <a href="#" class="xyz">Link</a>
 <a href="#" class="xyz">Link</a>
</p>

</body>
</html>
JavaScript

Avatar of undefined
Last Comment
Gurvinder Pal Singh

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Gurvinder Pal Singh

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23