#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

JavaScript events

An event is a signal from the browser that something has happened: the user clicked, typed, scrolled or resized the window, a form was submitted, an image finished loading, or a network request completed. JavaScript lets you listen for these signals and respond. Understanding which events exist and how they travel through the page helps you choose the right one and attach your listener in the right place.

Common event families

CategoryExamplesFired when
Mouse and pointerclick, dblclick, pointerdown, mouseoverA user clicks, taps or moves the pointer
Keyboardkeydown, keyupA key is pressed or released
Forminput, change, submit, focus, blurFields change, gain or lose focus, or a form is sent
Document and windowDOMContentLoaded, load, resize, scrollThe page loads or the viewport changes
Clipboard and dragcopy, paste, dragstart, dropContent is copied, pasted or dragged

Pointer events such as pointerdown cover mouse, touch and pen with one set of listeners, which makes them a good choice for interfaces that must work on phones.

How an event travels: capture and bubble

When you click a button inside a list inside a section, the click does not belong only to the button. The event first travels down from the window to the button (the capture phase), reaches the button (the target phase), and then travels back up through every ancestor (the bubble phase). Listeners run during the bubble phase by default, so a click listener on the section also fires when the button is clicked.

Calling event.stopPropagation() stops the event from moving further. Use it sparingly, because other code, such as a dropdown that closes when you click outside it, may rely on the event reaching the document.

Event delegation

Bubbling makes a powerful pattern possible: instead of adding a listener to every item, add one to the parent and check where the event came from. This also works for items added later.

const todoList = document.querySelector('#todos');

todoList.addEventListener('click', event => {
  const removeBtn = event.target.closest('button.remove');
  if (!removeBtn || !todoList.contains(removeBtn)) return;
  removeBtn.closest('li').remove();
});

document.addEventListener('keydown', event => {
  if (event.key === 'Escape') {
    document.querySelector('.modal')?.classList.remove('open');
  }
});

event.target may be an icon or span inside the button, so closest() walks up to find the button itself.

Events that do not bubble, and other surprises

Not every event bubbles. focus and blur stay on their element; use focusin and focusout if you want to delegate them. mouseenter and mouseleave also do not bubble, unlike mouseover and mouseout.

Events such as scroll, resize, input and pointermove can fire dozens of times per second. Heavy work inside them makes a page feel sluggish. Keep those handlers light, or limit how often they run with a debounce or throttle function.

Also note that keypress is deprecated. Use keydown and read event.key.

Main ideas

  • Pick the event that matches the user action precisely.
  • Events move down, hit the target, then bubble up.
  • Delegate to a parent for lists and dynamic content.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course