🔰remove handler
JS ⟩ browser ⟩ event ⟩ handler ⟩ register ⟩ remove
If you want to remove a capturing event handler, you must also pass true as the third argument to removeEventListener().
To remove a handler we need exactly the same function as was assigned.
// ❌ this won't work❗️
// ⭐️ same code but different functions ↴
// ╭────────────────────╮
elem.addEventListener ("click", () => alert('Thanks!'));
elem.removeEventListener("click", () => alert('Thanks!'));
// ✅ the right way
// same function ↴
function handler(){ ... } // ╭─────╮
elem.addEventListener ("click", handler);
elem.removeEventListener("click", handler);
Last updated
Was this helpful?