English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
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.
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 ‹/›
IEFirefoxOperaChromeSafari
All major browsers support the onclick event attribute
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>.
None.
<element onclick="script">
Value | Description |
---|---|
script | Specifies the script to be executed when the onclick event is triggered. |