๐ฐ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?