Sandra Schlichting Sandra Schlichting - 1 year ago 41
Javascript Question

Detect which link was clicked? Always returns undefined. What's wrong?

I am trying to detect which of the first 3 links is being clicked on by outputting the links ID.

It always returns


What's wrong?


<script src=""></script>

<script type="text/javascript">
window.onload = function() {

onclick = function() {
return false;


<a class="a" name="a" id="1" href="#">---1---</a>
<a class="a" name="a" id="2" href="#">---2---</a>
<a class="a" name="a" id="3" href="#">---3---</a>

<a href="#"> normal link </a>


Answer Source

You are not targeting any of the links.

  window.onload = function() {
    $("a.a").click(function() {
      return false;

What this is doing ($("a.a").click(function(){) is looking for any click events on anchors of class name 'a' and run the following anonymous function.