JavaScript Events Programs
Make web pages respond to people with JavaScript events. Handle clicks, typing, forms, mouse moves, drag and drop and custom events, with expected output.
10 programs · open one, then the next
Start with DOM Events Basics- 01DOM Events BasicsBeginnerLearn DOM events in JavaScript with addEventListener, named and arrow handlers, removeEventListener and the event object. Simple explanation and output.Open example →js// Method 1: Inline event handler (not recommended) // <button onclick="handleClick()">Click</button> // Method 2: addEventListener (recommended)
- 02Custom EventsIntermediateCreate and dispatch custom events in JavaScript with CustomEvent, detail data, bubbles and dispatchEvent. Simple explanation, use cases and the output.Open example →js// Method 1: Create custom event const customEvent = new CustomEvent('myCustomEvent', { detail: { message: 'Hello from custom event', data: 123 }, bubbles: true,
- 03Event Bubbling and CapturingIntermediateUnderstand event bubbling and capturing in JavaScript: the three phases, the capture flag and stopPropagation, with a clear explanation and output.Open example →js// Event Bubbling (default) // Event flows from target to root const parent = document.getElementById('parent');
- 04Event DelegationIntermediateLearn event delegation in JavaScript: one parent listener for many children with event.target, matches() and closest(). Explanation and output.Open example →js// Method 1: Basic event delegation const list = document.getElementById('myList'); // Instead of adding listener to each item
- 05Keyboard EventsBeginnerHandle keyboard events in JavaScript with keydown and keyup, e.key and e.code, modifier keys, preventDefault and a shortcut map. Clear explanation and output.Open example →js// Method 1: Keydown event document.addEventListener('keydown', function(e) { console.log('Key pressed:', e.key); console.log('Key code:', e.keyCode);
- 06Mouse EventsBeginnerHandle mouse events in JavaScript: click, dblclick, mousemove positions, enter and leave, buttons, right click and drag detection. Explanation and output.Open example →js// Method 1: Click events const button = document.getElementById('myButton'); button.addEventListener('click', function(e) {
- 07Form EventsBeginnerHandle form events in JavaScript: submit with preventDefault, input vs change, focus and blur, checkbox, radio, reset and invalid. Explanation and output.Open example →js// Method 1: Form submission const form = document.getElementById('myForm'); form.addEventListener('submit', function(e) {
- 08Window EventsIntermediateHandle window events in JavaScript: load vs DOMContentLoaded, beforeunload, resize, scroll with debounce, online and hashchange. Explanation and output.Open example →js// Method 1: Load event window.addEventListener('load', function() { console.log('Page fully loaded'); // All resources loaded
- 09Touch EventsIntermediateHandle touch events in JavaScript for mobile: touchstart, touchmove, touchend, swipe, pinch zoom and long press detection, with explanation and output.Open example →js// Method 1: Basic touch events const element = document.getElementById('myElement'); element.addEventListener('touchstart', function(e) {
- 10Drag and Drop EventsAdvancedBuild drag and drop in JavaScript with the HTML5 API, dataTransfer, a custom mouse and touch drag, position limits and a sortable list. Explanation and output.Open example →js// Method 1: HTML5 Drag and Drop API const draggable = document.getElementById('draggable'); const dropzone = document.getElementById('dropzone');