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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:
Before:

Click Here

After:

Welcome to GeeksforGeeks!

Example 2: Use button tag to call the event.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:
BERJAYA

Supported Browser: The browser supported by onclick event attribute are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Safari
  • Opera


My Personal Notes arrow_drop_up

BERJAYA
Check out this Author's contributed articles.

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.