Javascript DOM onmousedown Event get which element was clicked

Description

Javascript DOM onmousedown Event get which element was clicked

View in separate window

<!DOCTYPE html>
<html>
<head>
<script>
function whichElement(e) {/*from  ww w. j av  a2  s . 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;
  document.getElementById("demo").innerHTML = "You clicked on a " + tname + " element.";
}
</script>
</head>
<body onmousedown="whichElement(event)">
<p id="demo"></p>
<p>Click somewhere in the document. </p>
<h3>This is a heading</h3>
<img border="0" src="image3.png" alt="Smiley" width="32" height="32">
<p>This is a paragraph.</p>

</body>
</html>



PreviousNext

Related