Mouse Events

Handle mouse interaction events

BeginnerTopic: Events Programs
Back

JavaScript Mouse Events Program

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

Try This Code
// Method 1: Click events
const button = document.getElementById('myButton');

button.addEventListener('click', function(e) {
    console.log('Clicked');
});

button.addEventListener('dblclick', function(e) {
    console.log('Double clicked');
});

// Method 2: Mouse position
const element = document.getElementById('myElement');

element.addEventListener('mousemove', function(e) {
    console.log('X:', e.clientX, 'Y:', e.clientY);
    console.log('Page X:', e.pageX, 'Page Y:', e.pageY);
    console.log('Offset X:', e.offsetX, 'Offset Y:', e.offsetY);
});

// Method 3: Mouse enter/leave
element.addEventListener('mouseenter', function(e) {
    console.log('Mouse entered');
    e.target.style.backgroundColor = 'yellow';
});

element.addEventListener('mouseleave', function(e) {
    console.log('Mouse left');
    e.target.style.backgroundColor = '';
});

// Method 4: Mouse over/out (bubbles)
element.addEventListener('mouseover', function(e) {
    console.log('Mouse over');
});

element.addEventListener('mouseout', function(e) {
    console.log('Mouse out');
});

// Method 5: Mouse down/up
element.addEventListener('mousedown', function(e) {
    console.log('Mouse button pressed:', e.button);
    // 0: left, 1: middle, 2: right
});

element.addEventListener('mouseup', function(e) {
    console.log('Mouse button released');
});

// Method 6: Context menu (right click)
element.addEventListener('contextmenu', function(e) {
    e.preventDefault();
    console.log('Right click prevented');
    // Show custom context menu
});

// Method 7: Mouse buttons
element.addEventListener('mousedown', function(e) {
    if (e.button === 0) {
        console.log('Left button');
    } else if (e.button === 1) {
        console.log('Middle button');
    } else if (e.button === 2) {
        console.log('Right button');
    }
    
    // Check which buttons are pressed
    console.log('Buttons:', e.buttons);
    // 1: left, 2: right, 4: middle
});

// Method 8: Drag detection
let isDragging = false;

element.addEventListener('mousedown', function(e) {
    isDragging = true;
    console.log('Drag started');
});

element.addEventListener('mousemove', function(e) {
    if (isDragging) {
        console.log('Dragging at:', e.clientX, e.clientY);
    }
});

element.addEventListener('mouseup', function(e) {
    if (isDragging) {
        isDragging = false;
        console.log('Drag ended');
    }
});
Output
// Output depends on mouse interaction

Understanding Mouse Events

Mouse events handle pointer interactions.

Event Types

click: Single click
dblclick: Double click
mousedown/mouseup: Button press/release
mousemove: Mouse movement
mouseenter/mouseleave: Enter/leave element
mouseover/mouseout: Over/out (bubbles)
contextmenu: Right click

Event Properties

clientX/clientY: Viewport coordinates
pageX/pageY: Document coordinates
offsetX/offsetY: Element coordinates
button: Which button (0=left, 1=middle, 2=right)
buttons: All pressed buttons

Use Cases

Interactive UI
Drag and drop
Hover effects
Click tracking

Best Practices

Use mouseenter/leave for hover
Prevent contextmenu for custom menus
Track drag state with flags

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 Mouse Events

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