Syntax

JavaScript Listen for an Event Syntax

In JavaScript, listen for an event: runs a function when an event happens — button.addEventListener("click", handler);

Quick Answer

Syntax
button.addEventListener("click", handler);
JavaScriptSyntax

Listen for an Event

Code example
button.addEventListener("click", handler);

Runs a function when an event happens.

Learn More

How to listen for an event in JavaScript

  1. Open your JavaScript file or editor.
  2. Copy the example, then replace its sample names and values for your code.

JavaScript Listen for an Event Syntax

Use button.addEventListener("click", handler);.

Related Syntax References

Frequently asked questions

What is the JavaScript syntax for listen for an event?

Use this pattern: button.addEventListener("click", handler);

Can I copy this JavaScript code example?

Yes. Use Copy code and adapt the example to your program.

Is this JavaScript syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify