What element are we clicking on?

  1. Block external links
  2. Intercepting Clicks
  3. Find the A tag

If you haven’t already, try clicking on a part of the page that isn’t a link. The URL will be “undefined”. That’s because most elements don’t have an href attribute. They don’t have a URL. But they can still be clicked on. So what element actually gets the click? Let’s find out.

function manageLinks(event) {

var link = event.target;

var url = link.href;

var tag = link.tagName;

window.alert("The url for " + tag + " is " + url);

return false;

}

image 2Each HTML element has a “tagName” property that is the name of that HTML tag. This version of the manageLinks function gets that tagName and displays it in the alert box. So now you should see alerts like “The url for P is undefined” or “The url for A is http://www.hoboes.com/Mimsy/Movies/”.

  1. Block external links
  2. Intercepting Clicks
  3. Find the A tag