Skip to content

Load JavaScript on button click | Execute JavaScript function

  • by

There are a few ways to handle events with HTML/DOM. Different ways are useful in different situations. The best and easiest way is to use the input tag and use the onClick attribute to execute the JavaScript function.

Defining it in the HTML:

<input id="clickMe" type="button" value="clickme" onclick="doFunction();" />

Example: How to Load JavaScript on button click

Using an HTML button to call a JavaScript function.

<!DOCTYPE html>
    <html>
    <head>
        <script>
            function doFunction(){
                alert("Executed JavaScript");
            }
            
        </script>
    </head>
    <body>   

        <input id="clickMe" type="button" value="clickme" onclick="doFunction();" />

    </body>
    </html>

Output:

Load JavaScript on button click

Do comment if you have another way to do it or have any doubts and suggestions.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *