DEV Community

Cover image for JSByte: JavaScript Event handlers
Shruti Kapoor
Shruti Kapoor

Posted on • Edited on

10 3

JSByte: JavaScript Event handlers

I will be sharing bite sized learnings about JavaScript regularly in this series. Follow along with me as I re-learn JavaScript. This series will cover JS fundamentals, browsers, DOM, system design, domain architecture and frameworks.


Event propagation

Specificity:
If a node has children and has an event handler attached to it, it is able to listen to not only its own events, but also events on its children.
However, if two DOM elements have a handler, the more specific one - the closest to the target, fires off first.

Targeting multiple elements
A node can have only one onclick attribute, so you can register only one handler this way.

A node can have multiple addEventListener, so you can attach multiple click handlers using addEventListener. It also gives access to removeEventListener to unsubscribe from the event.

When you have an event handler that needs to be attached to multiple elements, attach the handler to the parent element instead of targeting each element individually.

<div class="buttons">
  <button>Press 1</button>
  <button>Press 2</button>
  <button>Press 3</button>
</div>
Enter fullscreen mode Exit fullscreen mode

const buttonContainer = document.querySelector('.buttons');
console.log('buttonContainer', buttonContainer);

buttonContainer.addEventListener('click', event => {
  console.log(event.target.value)
})
Enter fullscreen mode Exit fullscreen mode

Common events

onclick
dblclick
mousedown
mouseup
mousemove
keydown
keyup
touchmove
touchstart
touchend
onload
onfocus
onblur
onerror
onscroll


Interested in more JSBytes? Sign up for the newsletter

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs