freecoding.school100% FREE · NO SIGNUP
The Old NetISSUE #43 of 60

event handlers · the old onclick way

Pixel PeteVSQuirks Mode
Pixel Pete saysThe old way to handle events was inline onclick attributes or el.onclick properties.

Early DHTML wired events two ways: inline (<button onclick="go()">) or via the property (el.onclick = fn). Both work but allow only one handler per event — assigning a new onclick overwrites the old. They also mix behavior into markup. They were superseded by addEventListener, which allows many handlers.

Power-ups you unlock

Quirks Mode attacks — common mistakes

Boss battleWire a click with el.onclick, then note why a second assignment replaces it.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;padding:20px"><button id="b">click</button><p id="o"></p>
<script>document.getElementById('b').onclick=()=>document.getElementById('o').textContent='handled (one handler max)';</script></body></html>
▶ Open the interactive comic issue
‹ SetAttribute · GetAttribute · RemoveAttributeAddEventListener · The Modern Way ›