js_events_examples
Modern JavaScript HTML DOM Event Handling
Section titled “Modern JavaScript HTML DOM Event Handling”Events are actions that occur in the browser, such as a user clicking a button, text being entered into an input field, or a page finishing loading. JavaScript allows you to “listen” for these events and execute code in response, making web pages interactive.
The modern and recommended way to handle events is using the addEventListener() method. This method allows you to attach one or more event handler functions to an HTML element without modifying the HTML attributes directly.
Key benefits of addEventListener():
- Allows adding multiple handlers for the same event.
- Provides more control over the event handling phases (capturing vs. bubbling).
- Makes it easier to remove event listeners using
removeEventListener(). - Separates JavaScript logic from HTML structure, leading to cleaner code.
Basic syntax: element.addEventListener(event_type, handler_function, useCapture);
Common Input Event Examples
Section titled “Common Input Event Examples”Input events are triggered by user interaction with form elements.
blur: When an element loses focus (e.g., user clicks away from an input field).
change: When the value of an element like <input>, <select>, or <textarea> has been committed by the user. For text inputs and textareas, this usually happens when the element loses focus after its value was changed. For select boxes, radio buttons, and checkboxes, it fires immediately when the selection changes.
input: Fires synchronously when the value of an <input>, <select>, or <textarea> element is changed. This is often preferred over change for real-time feedback as the user types.
focus: When an element receives focus (e.g., user clicks on or tabs to an input field).
select: When some text has been selected in an <input> or <textarea>.
submit: When a form is submitted (typically by clicking a submit button or pressing Enter in a field). Handle this on the <form> element itself.
reset: When a form’s reset button is clicked. Handle this on the <form> element.
keydown: When a key is pressed down.
keypress: (Largely deprecated, consider keydown or input instead) Fired when a key that produces a character value is pressed down.
keyup: When a key is released.
The Event Object: Within an event handler function, an event object is automatically passed as the first argument. This object contains valuable information about the event, such as event.target (the element that triggered the event), event.key (for keyboard events), event.preventDefault() (to stop default browser actions), and event.stopPropagation() (to stop event bubbling).
Mouse Event Examples
Section titled “Mouse Event Examples”Mouse events are triggered by user interactions with a mouse or similar pointing device.
mouseover / mouseout: mouseover fires when the pointer moves onto an element or one of its children. mouseout fires when the pointer moves out of an element or one of its children. These events bubble.
mouseenter / mouseleave: Similar to mouseover/mouseout, but mouseenter and mouseleave do not bubble and are often preferred for simplicity when dealing with nested elements.
mousedown / mouseup: mousedown fires when a mouse button is pressed down on an element. mouseup fires when a mouse button is released over an element.
mousemove: Fires continuously as the mouse pointer moves over an element.
Click Event Examples
Section titled “Click Event Examples”click: Fires when a pointing device button (usually primary button) is pressed and released on a single element.
dblclick: Fires when a pointing device button is rapidly clicked twice on a single element.
contextmenu: Fires when the right mouse button is clicked (or equivalent action) to open a context menu.
Load and Window Event Examples
Section titled “Load and Window Event Examples”load: Fires on a resource (like an image, script, or the window itself) after it and all its dependent resources have finished loading.
DOMContentLoaded: Fires on the document when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. This is often preferred over window.onload for faster script execution.
error: Fires when a resource (e.g., an image or script) fails to load.
unload / beforeunload: beforeunload fires when the window, document and its resources are about to be unloaded. It can be used to prompt the user if they want to leave the page. unload fires when the document is being unloaded. Their use is sometimes restricted by browsers for user experience reasons.
resize: Fires on the window when the browser window has been resized.
scroll: Fires when the document view or an element has been scrolled. Can be attached to window or scrollable elements.
Further Exploration: Event Properties
Section titled “Further Exploration: Event Properties”Understanding properties of the event object is key to effective event handling:
Key pressed (e.g., event.key, event.code): Useful for keyboard events to determine which key was pressed.
Cursor coordinates (e.g., event.clientX, event.clientY, event.pageX, event.pageY): Provides mouse pointer position relative to the viewport or document.
Modifier keys (e.g., event.shiftKey, event.altKey, event.ctrlKey, event.metaKey): Checks if modifier keys were pressed during an event.
Event type (event.type): A string indicating the type of event that occurred (e.g., “click”, “mouseover”).
These examples provide a starting point. Real-world applications often involve more complex event handling, including event delegation, custom events, and interaction with asynchronous operations.