Syntax: element.addEventListener(event, function, useCapture);
JavaScript Tutorial
JavaScript Tutorial
JavaScript Asynchronous
JavaScript HTML DOM
Advertisement
Google AdSense Slot (LEADERBOARD)
Automated High-CTR Ad Rail • Set Publisher ID in config/adsense.php
Listeners
7 min read
•
Chapter 22 of 22
JS Event Listeners (addEventListener)
The addEventListener() method attaches an event handler to the specified element without overwriting existing event handlers.
Example
<!DOCTYPE html>
<html>
<body style="font-family:sans-serif;padding:20px;">
<button id="myBtn" style="padding:10px 16px;background:#04aa6d;color:white;border:none;border-radius:6px;cursor:pointer;">Click with EventListener</button>
<p id="res"></p>
<script>
document.getElementById("myBtn").addEventListener("click", () => {
document.getElementById("res").innerText = "Button clicked via addEventListener!";
});
</script>
</body>
</html>
Try it Yourself >>
Opens live sandboxed code editor
Advertisement
Google AdSense Slot (IN-ARTICLE)
Automated High-CTR Ad Rail • Set Publisher ID in config/adsense.php
Community Q&A & Discussion
Verified
No questions yet. Be the first to start the discussion!