DOM Events Basics

Basic DOM event handling

BeginnerTopic: Events Programs
Back

JavaScript DOM Events Basics Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// Method 1: Inline event handler (not recommended)
// <button onclick="handleClick()">Click</button>

// Method 2: addEventListener (recommended)
const button = document.getElementById('myButton');

button.addEventListener('click', function(event) {
    console.log('Button clicked!');
    console.log('Event:', event);
    console.log('Target:', event.target);
});

// Method 3: Named function
function handleClick(event) {
    console.log('Clicked!', event.target);
}

button.addEventListener('click', handleClick);

// Method 4: Arrow function
button.addEventListener('click', (e) => {
    console.log('Arrow function handler');
});

// Method 5: Multiple events
button.addEventListener('click', handleClick);
button.addEventListener('mouseenter', function() {
    console.log('Mouse entered');
});
button.addEventListener('mouseleave', function() {
    console.log('Mouse left');
});

// Method 6: Remove event listener
button.removeEventListener('click', handleClick);

// Method 7: Event object properties
button.addEventListener('click', function(e) {
    console.log('Type:', e.type);
    console.log('Target:', e.target);
    console.log('Current Target:', e.currentTarget);
    console.log('Timestamp:', e.timeStamp);
    console.log('Bubbles:', e.bubbles);
    console.log('Cancelable:', e.cancelable);
});
Output
// Output depends on user interaction

Understanding DOM Events Basics

DOM events allow interaction with web pages.

Event Types

click: Mouse click
mouseenter/mouseleave: Mouse hover
keydown/keyup: Keyboard
submit: Form submission
load: Page loaded
scroll: Page scrolling

addEventListener

Preferred method
Can add multiple handlers
More flexible

Event Object

type: Event type
target: Element that triggered
currentTarget: Element with handler
preventDefault(): Stop default action
stopPropagation(): Stop bubbling

Best Practices

Use addEventListener
Remove listeners when done
Use event delegation for dynamic content

Let us now understand every line and the components of the above program.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Practical Learning Notes for DOM Events Basics

This JavaScript program is part of the "Events Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents