20 JUL

eleven.eight The value of which when using addEventListener()

by Lottesco

eleven.eight The value of which when using addEventListener()

Within this book I take advantage of this new argument name experience so you can site the event target. In reality you need one title you like and its own quite normal observe e otherwise evt.

The value of this inside of the event listener function passed to the addEventListener() method will be a reference to the node or object the event is attached too. In the code below I attach an event to a

and then using this inside of the event listener gain access to the

element the event is attached too.

When events are invoked as part of the event flow the this value will remain the value of the node or object that the event listener is attached too. In the code below we add a click event listener to the and regardless of if you click on the

or the the value of this always points to .

Likewise its potential with the experience.currentTarget assets to obtain the same source, into node otherwise object invoking the function listener, the this property provides. On the code below We influence the event.currentTarget skills target property showcasing so it production a comparable really worth that.

11.8 Referencing the prospective from a meeting and not the fresh new node otherwise target the function are invoked to your

Because of the event flow its possible to click a

, contained inside of a element and have a click event listener attached to the element get invoked. When this happens, the event object passed to the event listener function attached to the provides a reference (i.e. event.target) to the node or object that the event originated on (i.e. the target). In the code below when the

is clicked, the element’s click event listener is invoked and the event.target property references the orginal

that was the target of the click event. The event.target can be extremely useful when an event that fires because of the event flow needs knowledge about the origin of the event.

Consider that in our code example if the element is clicked instead of the

then the event target and the element node that the event listener is invoked on are the same. Therefore event.target, this, and event.currentTarget will all contain a reference to the element.

11.9 Cancelling standard web browser occurrences playing with preventDefault()

Browsers provide several events already wired up when an HTML page is presented to a user. For example, clicking a link has a corresponding event (i.e. you navigate to a url). So does clicking a checkbox (i.e. box is checked) or typing text into a text field (i.e. text is inputed and appears on screen). These browser events can be prevented by calling the preventDefault() method inside of the event handler function associated with a node or object that invokes a browser default event. In the code below I prevent the default event that occurs on a , , and

© 2019 The Lottes Companies | Alpine Design