🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #151

Manage Events

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>

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>

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


Blocking Events

Example

<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>

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Manage Events – FAQs

Quick answers about learning Manage Events in JavaScript.

This free note from CodingNow 2.0 explains Manage Events in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including Manage Events, is 100% free with no signup required.
With focused practice, most students grasp Manage Events in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now