?
Solved

jQuery: Number of element clicked on

Posted on 2011-10-28
2
Medium Priority
?
299 Views
Last Modified: 2012-05-12
I want to know the number of the matched elements click on.  


In the example below, if a user clicks "Hello" then the alert should be "1".
If a user clicks "world" then the alert should be "2".
If a user clicks "test" then the alert should be "3".

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
  $('a').click(function() {
    alert( You clicked on link number ??? );
  });
});
</script>

</head>
<body>
<p>
 <a href="#">Hello</a>
 <a href="#">world</a>
 <a href="#">Test</a>
 <a href="#">Link</a>
</p>
</body>
</html>

Open in new window

0
Comment
Question by:hankknight
2 Comments
 
LVL 61

Accepted Solution

by:
HainKurt earned 1200 total points
ID: 37044543
index start from 0, you can add 1 if you wish
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
  $("#myLinks a").each(function(index){
  $(this).click(function(){
    alert("You clicked a:" + index);
  })
});
});
</script>

</head>
<body>
<p id=myLinks>
 <a href="#">Hello</a>
 <a href="#">world</a>
 <a href="#">Test</a>
 <a href="#">Link</a>
</p>
</body>
</html>

Open in new window

0
 
LVL 83

Assisted Solution

by:leakim971
leakim971 earned 800 total points
ID: 37044571
var link_number = $(this).index($(this).parent().find("a")) + 1;

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question