The answer to your IT questions
 

Advanced search  • Search tips
My QUESTIONS & ANSWERS
 Question:

Find the clicked anchor

User Asked by: alexander
Published on: 13:26/09.01.2008
Status: OPEN
How can i find which element was clicked in a structure like this:

<ul id="menu">
    <li><a>link 1</a></li>
    <li><a>link 2</a></li>
    <li><a>link 3</a></li>
</ul>
 Answers: 1
Sort by: /\ date | rating
Image Answer by: rostislav
Posted on: 13:26/09.01.2008
Rating: 1.8 from possible 5 with 5 votes
Try the following one:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript">
            function findClicked(event) {
                var target = event.target || window.event.srcElement;
                if (target.tagName == 'A') {
                    alert(target.innerHTML + ' is clicked.');
                }
            }           
        </script>
    </head>
    <body>
        <ul id="menu" onclick="findClicked(event)">
            <li><a href="#">link 1</a></li>
            <li><a href="#">link 2</a></li>
            <li><a href="#">link 3</a></li>
        </ul>      
    </body>
</html>
Vote:

Please vote! Your opinion matters!
If you haven't found what you've looking for, post a question
  
| Home | Hall of fame | Register | Log in | Terms of service | Help | Contacts |