JavaScript > Events

OnClick in JavaScript

How to execute a JavaScript function on click of the element?


In case we have to execute a JavaScript function on click of the element, we can use this approach.

    <script>
        function TestFunction() {
            alert("Click event fired.")
        }
    </script>

    <input type="button" id="btnTest" onclick="TestFunction()" value="Click me" />

In the above code snippet, TestFunction() fires when user clicks on the "Click me" button.

OUTPUT

 Views: 7026 | Post Order: 31



Write for us






Hosting Recommendations