Get the element name you clicked on - Javascript DOM Event

Javascript examples for DOM Event:onmousedown

Description

Get the element name you clicked on

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script>
function whichElement(e) {/*from   ww w.j a  v a2s  .  c  o m*/
    var targ;
    if (!e) {
        var e = window.event;
    }
    if (e.target) {
        targ = e.target;
    } else if (e.srcElement) {
        targ = e.srcElement;
    }
    var tname;
    tname = targ.tagName;
    console.log("You clicked on a " + tname + " element.");
}
</script>
</head>
<body onmousedown="whichElement(event)">

<h3>This is a heading</h3>
<img border="0" src="http://java2s.com/resources/a.png" alt="Smiley" width="32" height="32">
<p>This is a paragraph.</p>

</body>
</html>

Related Tutorials