English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML Reference Manual

Complete List of HTML Tags

HTML onclick event attribute

When a user clicks an element, a click event is triggered. During the entire process of each click, the execution order of the click event is after the mousedown and mouseup events.

HTML Event Attributes

Note: When using the click event to trigger an action, also consider adding this action to the keydown event to allow users who do not use a mouse or touchscreen to perform the same operation.

Online Example

Execute JavaScript when clicking the button:

<!DOCTYPE html>
<html>
<head>
<title>HTML onclick event attribute usage (Basic Tutorial Website oldtoolbag.com)</title>
<script>
function demo_click()
{
    alert(&39;Hey, executed the onclick event');
}
</script>
</head>
<body>
<button onclick="demo_click()">Try Me</button>
<p>Triggers a function when the button is clicked. The function will pop up an alert box.</p>
</body>
</html>
Test it out ‹/›

Browser Compatibility

IEFirefoxOperaChromeSafari

All major browsers support the onclick event attribute

Definition and Usage

The onclick attribute is triggered when the mouse clicks on an element.

Note: The onclick attribute cannot be applied to the following elements: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, or <title>.

HTML 4.01 with HTML5differences

None.

Syntax

<element onclick="script">

Attribute Value

ValueDescription
scriptSpecifies the script to be executed when the onclick event is triggered.
HTML Event Attributes