a.addEventListener("click", WhichLinkWasClicked());

Sorry , stumped again:
Uncaught SyntaxError: Unexpected identifier
“Function WhichLinkWasClicked(evt) {”

<!DOCTYPE html>
<html>
<body>

<p>This example uses the addEventListener() method to catch which kink was clicked.</p>

<a href="https://www.google.com">Visit google.com!</a>
<a href="https://www.duckduckgo.com">Visit duckduckgo.com</a>

<script>
  a.addEventListener("click", WhichLinkWasClicked());

Function WhichLinkWasClicked(evt) {
//        alert("Function WhichLinkWasClicked(evt)") ;
//	    alert(evt) ; 
		alert(evt.target.innerText);
		}
</script>

</body>
</html>

You should lowercase the Function to function. You also don’t need the open/closing parenthesis as part of WhichLinkWasClicked in the event listener. This will work:

a.addEventListener("click", WhichLinkWasClicked);

Lastly, you are calling addEventListener on a, but a isn’t actually defined in your code. You need to reference it via querySelector or querySelectorAll. For what you are trying to do, where you have two elements, it is slightly more involved. I encourage you to read the following three articles to get a better idea of how to make this work: Finding Elements in the DOM, Introduction to Events, Handling Events for Multiple Elements.

:stuck_out_tongue:

Thanks Kirupa & for the links :slight_smile:

1 Like