bhylu.com

Advertisements

Event Listener

AddEventListener lets multiple handlers observe an event without overwriting an existing handler property.

Where to run this

This example changes a document. Open it in the HTML playground, then select Run code. It runs inside a separate preview.

A practical example

HTML + JavaScript

<button id="count" type="button">Add one</button>
<p id="total">0</p>
<script>
  let count = 0;
  document.getElementById("count").addEventListener("click", () => {
    count += 1;
    document.getElementById("total").textContent = count;
  });
</script>

What happens

Each click increases the displayed count by one.