Element.addEventListener()
Listen for events on an element. You can find a full list of available events on the Mozilla Developer Network.
var btn = document.querySelector('#click-me');
btn.addEventListener('click', function (event) {
console.log(event); // The event details
console.log(event.target); // The clicked element
});
If an event does not bubble and you’re trying to use event delegation, pass in true
for the third argument, useCapture
.
document.addEventListener('focus', function (event) {
console.log('The following element just came into focus: ' + event.target);
}, true);