Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Scope JS Dates JS Arrays JS Sets JS Maps JS Math JS RegExp JS Data Types JS Errors JS Events JS Conventions JS References JS Versions

JS Advanced

JS Functions JS Objects JS Classes JS Iterations JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS HTML DOM JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript Event Management

Event Management is about adding, removing, and controlling events.


Adding Events

Example

<button id="btn">Click</button>

<p id="demo"></p>

<script>
const btn = document.getElementById("btn");

// Let btn listen for click
btn.addEventListener("click", myFunction);

function myFunction() {
  document.getElementById("demo").innerHTML = "Clicked!";
}
</script>
Try it Yourself »

Removing Events

Example

<button id="add">Add</button>
<button id="remove">Remove</button>
<button id="test">Test click</button>

<p id="demo"></p>

<script>
const test = document.getElementById("test");
const remove = document.getElementById("remove");
const add = document.getElementById("add");

function myFunction() {
   document.getElementById("demo").innerHTML += "Hello!";
}

// Let add listen for click
add.addEventListener("click", function () {
  // Let test listen for click
  test.addEventListener("click", myFunction);
});

// Let remove listen for click
remove.addEventListener("click", function () {
  // Prevent test from listen for click
  test.removeEventListener("click", myFunction);
});
</script>
Try it Yourself »

Note

To remove an event listener, you must use the same named function you used to add it.



Block Events

Example

Prevent default (block a link)

<a href="https://www.w3schools.com" id="link">Go to W3Schools</a>

<p id="demo"></p>

<script>
const link = document.getElementById("link");

// Let link listen for click
link.addEventListener("click", function (event) {
  event.preventDefault();
  document.getElementById("demo").innerHTML = "Link blocked!";
});
</script>
Try it Yourself »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.