CSS Portal

HTML onauxclick Event Attribute

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The onauxclick attribute in HTML is an event handler that triggers a JavaScript function when a user clicks on a mouse button other than the primary (usually left) button, typically the middle button (wheel) or right button. It is a modern alternative to using onclick for non-primary buttons, allowing more precise handling of auxiliary mouse clicks.

Basic Syntax
<element onauxclick="javascriptFunction(event)">
  • element: Any HTML element that can respond to mouse events (e.g., <button>, <div>, <a>).
  • javascriptFunction(event): The JavaScript code or function to execute when the event occurs. The event object gives you access to additional information, such as which mouse button was pressed.
Event Object Properties

When onauxclick is triggered, it passes a MouseEvent object with useful properties:

  • event.button → Indicates which mouse button was pressed:

    • 0 = Primary (left) button
    • 1 = Auxiliary (middle/wheel) button
    • 2 = Secondary (right) button
  • event.ctrlKey, event.shiftKey, event.altKey → Boolean values indicating if modifier keys were pressed.

  • event.clientX / event.clientY → Mouse cursor coordinates.

Key Points
  1. Purpose: Unlike onclick, which primarily handles left-clicks, onauxclick allows you to respond to middle and right mouse clicks without interfering with contextmenu or default behaviors.
  2. Browser Support: Modern browsers support it, including Chrome, Edge, and Firefox. However, some older browsers may not recognize it.
  3. Default Behavior: Clicking the middle button usually opens links in a new tab. You can prevent this using event.preventDefault() inside the handler.
Example 1: Basic Usage
<button onauxclick="alert('Auxiliary button clicked!')">
  Click me with middle or right mouse button
</button>
  • Left-click does nothing.
  • Middle or right-click triggers the alert.
Example 2: Detecting Which Button Was Clicked
<div onauxclick="handleAuxClick(event)" style="padding:20px; border:1px solid black; width:200px; text-align:center;">
  Right or Middle Click Me
</div>

<script>
function handleAuxClick(event) {
    if (event.button === 1) {
        alert('Middle button clicked!');
    } else if (event.button === 2) {
        alert('Right button clicked!');
    }
    event.preventDefault(); // prevent default actions like opening context menu
}
</script>
  • This example detects whether the middle or right mouse button was clicked and prevents the browser’s default action.
Use Cases
  • Opening links in a custom way when middle-clicked.
  • Custom context menus without relying solely on contextmenu.
  • Detecting middle-clicks for custom actions like opening tabs or copying data.
Difference Between onclick, oncontextmenu, and onauxclick
Attribute Triggered By
onclick Primary (left) mouse button
oncontextmenu Right mouse button
onauxclick Middle or other non-primary buttons

Syntax

<element onauxclick="script(event)">

Values

  • scriptThe name of the script to use when the event has been triggered.

Example

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>onauxclick Example</title>
<style>
.box {
width: 200px;
height: 100px;
border: 2px solid #333;
display: flex;
align-items: center;
justify-content: center;
margin: 50px;
user-select: none;
cursor: pointer;
}
</style>
</head>
<body>

<div class="box" onauxclick="handleAuxClick(event)">
Click me with Middle or Right Mouse Button
</div>

<script>
function handleAuxClick(event) {
if (event.button === 1) {
alert('Middle button clicked!');
} else if (event.button === 2) {
alert('Right button clicked!');
} else {
alert('Other button clicked!');
}

event.preventDefault(); // Prevent default behavior like opening context menu
}
</script>

</body>
</html>

Browser Support

The following information will show you the current browser support for the HTML onauxclick event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.

This event attribute is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 27th December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!