HTML | onclick Event Attribute
The onclick event attribute works when the user click on the button. When mouse clicked on the element then the script run.
Syntax:
<element onlick = "script">
Attribute Value: This attribute contains single value script which works when mouse clicked on the element.
Supported Tags: This attribute is supported by all HTML tags except <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>.
Example 1:
<!DOCTYPE HTML> <html> <head> <title> onclick event attribute </title> <script> /* If onclick event call then this script will run */ function onclick_event() { document.getElementById("geeks").innerHTML = "Welcome to GeeksforGeeks!"; } </script> </head> <body style = "text-align:center"> <!-- onclick event call here --> <p id = "geeks" onclick = "onclick_event()"> Click Here </p> </body> </html> |
Output:
Before:
Click Here
After:
Welcome to GeeksforGeeks!
Example 2: Use button tag to call the event.
<!DOCTYPE html> <html> <head> <title> onclick event attribute </title> <script> function funonclick() { document.getElementById("geeks").innerHTML = "Welcome to GeeksforGeeks!"; } </script> </head> <body style = "text-align:center"> <!-- onclick event call here --> <button onclick = "funonclick()"> Click Here </button> <p id = "geeks"></p> </body> </html> |
Output:

Supported Browser: The browser supported by onclick event attribute are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Safari
- Opera
Recommended Posts:
- How to stop event propagation with inline onclick attribute in JavaScript ?
- HTML | DOM onclick Event
- HTML | onbeforeunload Event Attribute
- HTML | onbeforeprint Event Attribute
- HTML | onafterprint Event Attribute
- HTML | ondrop Event Attribute
- HTML | onpageshow Event Attribute
- HTML | onsearch Event Attribute
- HTML | onhashchange Event Attribute
- HTML | onerror Event Attribute
- HTML | onkeyup Event Attribute
- HTML | onwheel Event Attribute
- HTML | onfocus Event Attribute
- HTML | onoffline Event Attribute
- HTML | oninvalid Event Attribute
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




