Skip to main content Accessibility Feedback

Event Listeners

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);

Works in all modern browsers, and IE9+. You can extend support back further with polyfills.