#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

Event types

An event is a signal the browser sends when something happens: a click, a key press, a form being submitted, the page finishing loading. Each kind of signal has a name, called its event type, such as click, keydown or submit. You pass that name to addEventListener to run code when it happens. Knowing which type to listen for is half the work of making a page interactive.

The main families of events

FamilyCommon typesFires when
Mouseclick, dblclick, mousedown, mouseup, mouseover, mouseout, mouseenter, mouseleave, contextmenuThe user uses a mouse or trackpad
Pointerpointerdown, pointermove, pointerupMouse, touch or pen input, handled by one API
Keyboardkeydown, keyupA key is pressed or released
Forminput, change, submit, resetField values change or a form is sent
Focusfocus, blur, focusin, focusoutAn element gains or loses focus
Document and windowDOMContentLoaded, load, resize, scroll, beforeunloadThe page loads, resizes, scrolls or is closing
Clipboardcopy, cut, pasteClipboard actions

Choosing between similar events

Several pairs look alike but behave differently, and picking the wrong one causes subtle bugs.

  • input vs change: input fires on every keystroke in a text field. change fires only when the value is committed, typically when the field loses focus. For live search, use input. For checkboxes and selects, change fires immediately on selection.
  • mouseover vs mouseenter: mouseover bubbles and fires again when moving between child elements. mouseenter does not bubble and fires once when entering the element.
  • focus vs focusin: focus and blur do not bubble, so a listener on a parent form will not hear them. focusin and focusout do bubble, which suits event delegation.
  • DOMContentLoaded vs load: the first fires when the HTML is parsed; the second waits for images, stylesheets and other resources too.

Reading details from the event object

Every listener receives an event object with the details of what happened.

const search = document.querySelector('#search');
const form = document.querySelector('#search-form');

search.addEventListener('input', (e) => {
  console.log('Typed so far:', e.target.value);
});

document.addEventListener('keydown', (e) => {
  if (e.key === 'Escape') closeModal();
  if (e.key === 'k' && (e.ctrlKey || e.metaKey)) {
    e.preventDefault();               // stop the browser's own shortcut
    search.focus();
  }
});

form.addEventListener('submit', (e) => {
  e.preventDefault();
  runSearch(search.value);
});

window.addEventListener('resize', () => {
  console.log('Width is now', window.innerWidth);
});

Mouse events add clientX and clientY for position. Keyboard events provide key (the character, like "a" or "Enter") and code (the physical key, like "KeyA").

Outdated habits to drop

Older tutorials use the keypress event and properties like keyCode or which. These are deprecated. Use keydown with event.key instead. Another habit is listening for click on a submit button instead of submit on the form; that misses users who submit by pressing Enter. Finally, scroll and resize can fire many times per second, so keep their handlers light or throttle them.

Takeaway

Match the event to the intention: input for live typing, change for committed choices, submit for forms, keydown with event.key for shortcuts, and the bubbling focusin/focusout when delegating. The next lesson moves on to the DOM itself.

Ready to master real-world web development?

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

Explore Course