How to addEventListener() in Javascript

The addEventListener() method attaches an event handler to the specified element – it is supported in many browsers such as Google Chrome, Mozilla Firefox, Safari, Opera, and the latest versions of Internet Explorer.

Syntax

element.addEventListener(event, function, useCapture)

event: Required parameter. Name of the event that is to be clicked. Data type is string.

function: Required parameter. The function is supposed to be executed when the event is clicked.

function can be both internal and external functions.

useCapture: Optional parameter. A boolean value indicates whether the event handler is supposed to be executed in the bubbling or the capturing phase.

if true:
  capturing phase
else:
  bubbling phase

Code

onclick()

mouseover

mouseout

Copyright ©2024 Educative, Inc. All rights reserved