HTMLify
removeEventListener.html
Views: 584 | Author: shubh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | // remove eventlistener <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>event listener</title> <style> body { background-color: aquamarine; } button{ background-color:aquamarine; border: 2px solid black; border-radius: 5px; cursor: pointer; } </style> </head> <body> <h1 id="heading"> hi javascript </h1> <button id="btn">Click</button> <script> //Note: to remove an eventlistener the callback reference should be same to remove. let btn = document.getElementById("btn"); btn.addEventListener("click", () =>{ console.log("button click1"); }); btn.addEventListener("click", () =>{ console.log("button click2"); }); const handler=() =>{ console.log("button click3"); }; btn.addEventListener("click",handler); btn.addEventListener("click", () =>{ console.log("button click4"); }); btn.removeEventListener("click",handler); </script> </body> </html> |