What are the 8 types of javascript events?

HTML events are "things" that happen to HTML elements.

When JavaScript is used in HTML pages, JavaScript can "react" on these events.

HTML Events

An HTML event can be something the browser does, or something a user does.

Here are some examples of HTML events:

  • An HTML web page has finished loading
  • An HTML input field was changed
  • An HTML button was clicked

Often, when events happen, you may want to do something.

JavaScript lets you execute code when events are detected.

HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.

With single quotes:

With double quotes:

In the following example, an title attribute [with code], is added to a element:

Example

The time is?

Try it Yourself »

In the example above, the JavaScript code changes the content of the element with id="demo".

In the next example, the code changes the content of its own element [using this.innerHTML]:

Example

The time is?

Try it Yourself »

JavaScript code is often several lines long. It is more common to see event attributes calling functions:

Common HTML Events

Here is a list of some common HTML events:

EventDescription
onchange An HTML element has been changed
title The user clicks an HTML element
onmouseover The user moves the mouse over an HTML element
onmouseout The user moves the mouse away from an HTML element
onkeydown The user pushes a keyboard key
onload The browser has finished loading the page

The list is much longer: W3Schools JavaScript Reference HTML DOM Events.

JavaScript Event Handlers

Event handlers can be used to handle and verify user input, user actions, and browser actions:

  • Things that should be done every time a page loads
  • Things that should be done when the page is closed
  • Action that should be performed when a user clicks a button
  • Content that should be verified when a user inputs data
  • And more ...

Many different methods can be used to let JavaScript work with events:

  • HTML event attributes can execute JavaScript code directly
  • HTML event attributes can call JavaScript functions
  • You can assign your own event handler functions to HTML elements
  • You can prevent events from being sent or being handled
  • And more ...

You will learn a lot more about events and event handlers in the HTML DOM chapters.

Test Yourself With Exercises

Exercise:

The element should do something when someone clicks on it. Try to fix it!

Click me.

Start the Exercise


Event typeDescriptionDocumentation
Animation

Events related to the Web Animation API.

Used to respond to changes in animation status [e.g. when an animation starts or ends].

Animation events fired on Document, Window, HTMLElement.
Asynchronous data fetching

Events related to the fetching data.

Events fired on AbortSignal, XMLHttpRequest, FileReader.
Clipboard

Events related to the Clipboard API.

Used to notify when content is cut, copied, or pasted.

Events fired on Document, Element, Window.
Composition

Events related to composition; entering text "indirectly" [rather than using normal keyboard presses].

For example, text entered via a speech to text engine, or using special key combinations that modify keyboard presses to represent new characters in another language.

Events fired on Element.
CSS transition

Events related to CSS Transitions.

Provides notification events when CSS transitions start, stop, are cancelled, etc.

Events fired on Document, HTMLElement, Window.
Database

Events related to database operations: opening, closing, transactions, errors, etc.

Events fired on IDBDatabase, IDBOpenDBRequest, IDBRequest, IDBTransaction.
DOM mutation

Events related to modifications to the Document Object Model [DOM] hierarchy and nodes.

Warning: Mutation Events are deprecated. Mutation Observers should be used instead.

Drag'n'drop, Wheel

Events related to using the HTML Drag and Drop API and wheel events.

Drag and Wheel events are derived from mouse events. While they are fired when using mouse wheel or drag/drop, they may also be used with other appropriate hardware.

Drag events fired on Document

Wheel events fired on Document and Element

Focus

Events related to elements gaining and losing focus.

Events fired on Element, Window.
Form

Events related to forms being constructed, reset and submitted.

Events fired on HTMLFormElement.
Fullscreen

Events related to the Fullscreen API.

Used to notify when the transitioning between full screen and windowed modes, and also of errors occurring during this transition.

Events fired on Document, Element.
Gamepad

Events related to the Gamepad API.

Events fired on Window.
Gestures

Touch events are recommended for implementing gestures.

Events fired on Document, Element.

In addition there are a number of non-standard gesture events:

  • Non-standard WebKit specific events on Element: gesturestart event, gesturechange event, gestureend event.
  • Non-standard IE specific events on Element: MSGestureStart, MSGestureChange, MSGestureEnd, MSGestureHold, MSGestureTap.
  • Mouse gesture events for Firefox Addons
History

Events related to the History API.

Events fired on Window.
HTML element content display management

Events related to changing the state of a display or textual element.

Events fired on HTMLDetailsElement, HTMLDialogElement, HTMLSlotElement.
Inputs

Events related to HTML input elements e.g. , , or .

Events fired on HTMLElement, HTMLInputElement.
Keyboard

Events related to using a keyboard.

Used to notify when keys are moved up, down, or just pressed.

Events fired on Document, Element.
Loading/unloading documents

Events related to loading and unloading documents.

Events fired on Document and Window.

Manifests

Events related to installation of progressive web app manifests.

Events fired on Window.
Media

Events related to media usage [including the Media Capture and Streams API, Web Audio API, Picture-in-Picture API, etc.].

Events fired on ScriptProcessorNode, HTMLMediaElement, AudioTrackList, AudioScheduledSourceNode, MediaRecorder, MediaStream, MediaStreamTrack, VideoTrackList, HTMLTrackElement, OfflineAudioContext, TextTrack, TextTrackList, Element/audio, Element/video.
Messaging

Events related to a window receiving a message from another browsing context.

Events fired on Window.
Mouse

Events related to using a computer mouse.

Used to notify when the mouse is clicked, doubleclicked, up and down events, right-click, movement in and out of an element, text selection, etc.

Pointer events provide a hardware-agnostic alternative to mouse events. Drag and Wheel events are derived from mouse events.

Mouse events fired on Element
Network/Connection

Events related to gaining and losing network connection.

Events fired on Window.

Events fired on NetworkInformation [Network Information API].

Payments

Events related to the Payment Request API.

Events fired on PaymentRequest, PaymentResponse.

Performance

Events related to High Resolution Time API, Performance Timeline API, Navigation Timing API, User Timing API, and Resource Timing API.

Events fired on Performance.

Pointer

Events related to the Pointer Events API.

Provides hardware-agnostic notification from pointing devices including Mouse, Touch, pen/stylus.

Events fired on Document, HTMLElement.
Print

Events related to printing.

Events fired on Window.
Promise rejection

Events sent to the global script context when any JavaScript promise is rejected.

Events fired on Window.
Sockets

Events related to the WebSockets API.

Events fired on Websocket.
SVG

Events related to SVG images.

Events fired on SVGElement, SVGAnimationElement, SVGGraphicsElement.

Text selection

Selection API events related to selecting text.

Event [selectionchange] fired on HTMLTextAreaElement, HTMLInputElement.

Touch

Events related to the Touch Events API.

Provides notification events from interacting with a touch sensitive screen [i.e. using a finger or stylus]. Not related to the Force Touch API.

Events fired on Document, Element.
Virtual reality

Events related to the WebXR Device API.

Warning: The WebVR API [and associated Window events] are deprecated.

Events fired on XRSystem, XRSession, XRReferenceSpace.
RTC [real time communication]

Events related to the WebRTC API.

Events fired on RTCDataChannel, RTCDTMFSender, RTCIceTransport, RTCPeerConnection.
Server-sent events

Events related to the server sent events API.

Events fired on EventSource.
Speech

Events related to the Web Speech API.

Events fired on SpeechSynthesisUtterance.
Workers

Events related to the Web Workers API, Service Worker API, Broadcast Channel API, and Channel Messaging API.

Used to respond to new messages and message sending errors. Service workers can also be notified of other events, including push notifications, users clicking on displayed notifications, that push subscription has been invalidated, deletion of items from the content index, etc.

Events fired on ServiceWorkerGlobalScope, DedicatedWorkerGlobalScope, SharedWorkerGlobalScope, WorkerGlobalScope, Worker, WorkerGlobalScope, BroadcastChannel, MessagePort.

What are the types of events in JavaScript?

Common HTML Events.

How many JavaScript events are there?

The standard HTML 5 events are listed here for your reference. Here script indicates a Javascript function to be executed against that event. ... HTML 5 Standard Events..

What is event in JavaScript explain any five different types of event handlers?

Event handlers.

What are JavaScript events Why are they useful?

Javascript has events that provide a dynamic interface to a webpage. These events are connected to elements in the Document Object Model[DOM]. Also, these events by default use the bubbling propagation i.e, upwards in the DOM from children to parent. We can bind events either as inline or in an external script.

Chủ Đề