target javascript code example
Example 1: event target javascript
Using the event.target property together with the element.tagName property to find out which element triggered a specified event:
<body onclick="myFunction(event)">
<p>Click on any elements in this document to find out which element triggered the onclick event.</p>
<h1>This is a heading</h1>
<button>This is a button</button>
<p id="demo"></p>
<script>
function myFunction(event) {
var x = event.target;
document.getElementById("demo").innerHTML = "Triggered by a " + x.tagName + " element";
}
</script>
Example 2: javascript .target
const ul = document.createElement('ul');
document.body.appendChild(ul);
const li1 = document.createElement('li');
const li2 = document.createElement('li');
ul.appendChild(li1);
ul.appendChild(li2);
function hide(evt) {
evt.target.style.visibility = 'hidden';
}
ul.addEventListener('click', hide, false);
Example 3: event.target javascript
const theTarget = someEvent.target;
Example 4: what is e.target in javascript
The target property gets the element on which the event originally occurred,
opposed to the currentTarget property, which always refers to the element
whose event listener triggered the event.