DOM Events Event Object


To access the event object, include an event parameter in the event listener callback function:

var foo = document.getElementById("foo");
foo.addEventListener("click", onClick);

function onClick(event) {
  // the `event` parameter is the event object
  // e.g. `event.type` would be "click" in this case



<div id="parent">
   <div id="child"></div>


var parent = document.querySelector('#parent');
var child = document.querySelector('#child');

child.addEventListener('click', function(e) {
   alert('child clicked!');

parent.addEventListener('click', function(e) {
   alert('parent clicked!');

since the child stops the event propagation, and the events are listened to during bubbling phase, clicking on the child will only trigger the child. without stopping the propagation both events will be triggered.


The event.preventDefault() method stops the default action of an element from happening.

For example:

  • Prevent a submit button from submitting a form
  • Prevent a link from following the URL
var allAnchorTags = document.querySelector('a');

allAnchorTags.addEventListener('click', function(e){
    console.log('anchor tags are useless now! *evil laugh*');
}); vs e.currentTarget

e.currentTarget Identifies the current target for the event, as the event traverses the DOM. It always refers to the element the event handler has been attached to as opposed to which identifies the element on which the event occurred.

in other words will return what triggers the event dispatcher to trigger

e.currentTarget will return what you assigned your listener to.


   <button id="my-button"></button>


var body = document.body;
body.addEventListener( 'click', function(e) {
    console.log('e.currentTarget', e.currentTarget);

if you click my-button,

  • will be my-button
  • e.currentTarget will be body