What Is the Auxclick Event?
The auxclick event represents a fundamental yet often overlooked aspect of mouse interaction on the web. While most developers are familiar with the standard click event triggered by the left mouse button, the auxclick event opens up possibilities for handling secondary mouse button interactions--specifically right-clicks and middle-clicks--in a clean, standardized way.
Introduced as part of the modern Pointer Events specification, auxclick addresses longstanding browser inconsistencies in how secondary button clicks were handled. Historically, developers relied on mousedown events with button detection logic to differentiate between mouse buttons, resulting in inconsistent behavior across browsers and platforms. The auxclick event provides a dedicated, predictable mechanism that works consistently regardless of the user's browser or operating system.
This standardization enables developers to create rich interactive experiences without worrying about cross-browser compatibility issues. Whether you're building a web application with custom context menus, a design tool with specialized mouse interactions, or a productivity platform that leverages middle-click for efficiency, auxclick provides the reliable foundation you need.
When to Use Auxclick
- Right-click context menus and custom context menus
- Middle-click interactions (opening links in new tabs)
- Detecting non-primary button clicks for specialized functionality
- Gaming interfaces and drawing applications
Event Firing Sequence
The auxclick event follows a specific firing order in the mouse event lifecycle:
- mousedown - Fires when a mouse button is pressed down
- mouseup - Fires when the pressed button is released
- auxclick - Fires only if the button was released on the same element and was a non-primary button
Distinguishing Auxclick from Click
Unlike the click event, which fires for the primary mouse button, auxclick specifically targets secondary buttons. The click event will still fire for the left button even when auxclick is used for right or middle clicks. This separation allows developers to handle different button types independently without complex button detection logic.
The auxclick event fires at an Element when a non-primary pointing device button--any mouse button other than the primary (usually leftmost) button--has been pressed and released both within the same element. According to MDN Web Docs on the auxclick event, this event was introduced to provide a reliable way to handle secondary mouse button interactions across different browsers and platforms.
Understanding this event sequence is essential for building responsive JavaScript applications that handle complex user interactions correctly.
Event Type and Properties
The auxclick event returns a PointerEvent object, which provides detailed information about the interaction. This inheritance chain--PointerEvent → MouseEvent → UIEvent → Event--means developers have access to a rich set of properties for customizing behavior.
Key PointerEvent Properties
| Property | Description |
|---|---|
| pointerId | Unique identifier for the pointer causing the event |
| pointerType | Indicates the device type (mouse, pen, touch) |
| pressure | Normalized pressure value (0-1) for touch/pen input |
| tiltX/tiltY | Angle information for stylus input |
| isPrimary | Whether this represents the primary pointer |
Button Detection
The MouseEvent properties button and buttons indicate which mouse button triggered the event:
- button = 0 - Primary button (usually left)
- button = 1 - Middle button (wheel click)
- button = 2 - Secondary button (right click)
This inheritance hierarchy enables code reuse and compatibility. Developers familiar with Pointer Events can apply their existing knowledge when working with auxclick, as the same event properties and methods are available.
Browser Compatibility
The auxclick event reached Baseline status in December 2024, meaning it works across the latest versions of all major browsers including Chrome, Edge, Firefox, and Safari. This broad support makes it safe to use in production applications without fallbacks.
Browser Support Status
- Chrome: Full support (all modern versions)
- Edge: Full support (all modern versions)
- Firefox: Full support (all modern versions)
- Safari: Full support (all modern versions)
Fallback Considerations
For applications supporting older browsers, developers should understand that:
- Earlier versions of browsers may have fired MouseEvent instead of PointerEvent
- Some browsers had inconsistent behavior with secondary button clicks
- Progressive enhancement strategies can provide graceful degradation
As documented by MDN Web Docs, the standardization of auxclick resolved years of inconsistent behavior that forced developers to implement complex workarounds for cross-browser compatibility.
For teams building cross-browser compatible web applications, this standardization represents a significant improvement in the web development ecosystem.
Code Examples
Basic Auxclick Handler
document.addEventListener('auxclick', (event) => {
// Check which button was clicked
switch (event.button) {
case 1: // Middle button
console.log('Middle button clicked');
event.preventDefault();
break;
case 2: // Secondary button
console.log('Right button clicked');
event.preventDefault();
break;
}
});
Custom Context Menu Pattern
const customMenu = document.getElementById('custom-context-menu');
document.addEventListener('contextmenu', (event) => {
event.preventDefault();
customMenu.style.display = 'block';
customMenu.style.left = `${event.pageX}px`;
customMenu.style.top = `${event.pageY}px`;
});
document.addEventListener('click', () => {
customMenu.style.display = 'none';
});
customMenu.addEventListener('auxclick', (event) => {
event.stopPropagation();
// Handle menu item selection
});
Handling Different Buttons on Specific Elements
const actionButton = document.getElementById('action-button');
actionButton.addEventListener('auxclick', (event) => {
if (event.button === 2) {
// Right-click behavior
showSecondaryMenu(event);
} else if (event.button === 1) {
// Middle-click behavior
openInBackgroundTab(event.target.href);
}
});
These patterns form the foundation for building interactive web interfaces that respond to different mouse button inputs. For additional code examples, explore the MDN DOM Examples repository which provides practical implementations of auxclick and related events.
Preventing Default Actions
Understanding when and how to prevent default actions is crucial for auxclick implementations.
Middle-Click Default Behavior
For most browsers, middle-click triggers opening a link in a new tab. This can be prevented:
element.addEventListener('auxclick', (event) => {
if (event.button === 1) { // Middle button
event.preventDefault();
// Custom behavior instead
}
});
Context Menu Default Behavior
The right-click context menu requires preventing the contextmenu event, not auxclick, due to operating system timing differences:
document.addEventListener('contextmenu', (event) => {
event.preventDefault();
// Show custom menu
});
Platform-Specific Considerations
Different operating systems map default behaviors to secondary buttons:
- Windows: Middle-click often triggers auto-scroll
- macOS: Secondary click shows context menu
- Linux: Varies by desktop environment
When building cross-browser compatible applications, always test your implementations across multiple platforms to ensure consistent behavior for all users.
Best Practices
Performance Considerations
- Attach event listeners to specific elements when possible
- Use event delegation for dynamic content
- Remove listeners when components unmount (in SPAs)
- Consider passive event listeners for scrolling scenarios
Accessibility
- Ensure keyboard alternatives exist for all auxclick interactions
- Don't override browser shortcuts without clear user communication
- Consider screen reader users who cannot perform mouse actions
- Provide visual feedback for all interactive states
User Experience
- Document custom behaviors for power users
- Consider providing settings for mouse button customization
- Avoid surprising behaviors that conflict with system conventions
- Test across different mouse devices and operating systems
Following these best practices ensures your web applications are performant, accessible, and provide excellent user experiences across all devices and input methods.
Conclusion
The auxclick event provides a standardized way to handle non-primary mouse button interactions on the web. With broad browser support now established, developers can confidently implement custom context menus, middle-click behaviors, and multi-button mouse interactions. Understanding the event's place within the broader mouse and pointer event ecosystem enables creation of sophisticated, accessible interactive experiences.
Related Events
The auxclick event exists within a broader ecosystem of pointer and mouse events:
- mousedown - Button pressed
- mouseup - Button released
- click - Primary button click completed
- auxclick - Non-primary button click completed
- contextmenu - Right-click requested
- dblclick - Double-click detected
Further Learning
Explore these related topics to deepen your understanding of JavaScript event handling:
- JavaScript Event Listeners - Master the fundamentals of event handling
- Pointer Events - Learn the unified input model for mouse, touch, and pen
Sources
Frequently Asked Questions
What is the difference between click and auxclick?
The click event fires only for the primary (left) mouse button, while auxclick fires for non-primary buttons like right-click and middle-click. This separation allows clean handling of different button types without complex button detection logic.
Does auxclick work on mobile devices?
The auxclick event is primarily designed for mouse input. On touch devices, consider using pointer events or touch events for similar functionality. The pointerType property can help distinguish device types.
Can I prevent the right-click context menu with auxclick?
No, you should use the contextmenu event to prevent the default right-click menu. The auxclick event fires after the contextmenu decision has been made by the browser.
Is auxclick supported in all modern browsers?
Yes, auxclick reached Baseline status in December 2024 and is fully supported in Chrome, Edge, Firefox, and Safari. Older browsers may need fallbacks or feature detection.