target Event Property - Using event.target with the element.tagName to find out which element triggered the event - Javascript DOM

Javascript examples for DOM:Event

Description

target Event Property - Using event.target with the element.tagName to find out which element triggered the event

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body onclick="myFunction(event)">

<p>Click me.</p>

<h1>This is a heading</h1>

<button>This is a button</button>

<p id="demo"></p>

<script>
function myFunction(event) {//from  w ww . j  av  a  2s  . com
    var x = event.target;
    document.getElementById("demo").innerHTML = "Triggered by a " + x.tagName + " element";
}
</script>

</body>
</html>

Related Tutorials