Cover Image for HTML Button onClick
68 views

HTML Button onClick

In HTML, you can use the onClick attribute to specify the action or JavaScript function that should be executed when a button is clicked. Here’s an example of how you can use the onClick attribute to define a button’s click event:

HTML
<button onClick="myFunction()">Click me!</button>

<script>
  function myFunction() {
    // Perform some action when the button is clicked
    alert("Button clicked!");
  }
</script>

In the above example, when the button is clicked, the myFunction() JavaScript function will be executed. In this case, it displays an alert box with the message “Button clicked!”.

You can replace myFunction() with the name of any custom JavaScript function you want to call when the button is clicked. You can include any desired functionality or actions within the function.

Note that the onClick attribute can also be used with other HTML elements such as <a> (anchor) tags and <input> tags of type “button” or “submit”. The concept remains the same: the specified function or action will be triggered when the element is clicked.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS