HTML Onclick Events

The HTML on-click events work on HTML elements and input tags. We can click on the input tag, button, and other HTML elements to operate the function. The function's script can be in either HTML or JavaScript code. If the script of the on-click events is complicated, use JavaScript; otherwise, use it in the HTML tag.

Syntax

The following syntax is used for HTML onclick events in HTML elements or tags.

Description

  • The event can use a script code or basic function operated in an HTML tag.
  • The event can use the function name and the function in JavaScript.
  • The onclick event attribute is supported by all HTML tags except <br>, <head>, <html>, <base>, <bdo>, <iframe>, <script>, <style>, <meta>, <param>, and <title>.

Examples

The following examples show the html onclick function with the simple script operation.

Example 1

The following examples operate on click event in html tag with the basic code. We can use the button for the script operation.

Output:

The output shows the on-click event on the button and its operation.

HTML Onclick Events

Example 2

The following examples operate an onclick event in html tag with the script code. We can use the input tag with the submit type for the script operation.

Output:

The output shows the onclick event on the input submit type and its operation.

HTML Onclick Events

Example 3

The following examples operate an onclick event in html input tag with the script code. We can use the input tag with text type for the script operation.

Output:

The output shows the onclick event on the input text type and its operation.

HTML Onclick Events

Example 4

The following examples operate an onclick event in html input tag with the script code. We can use the input tag with text type for the script operation.

Output:

The output shows the onclick event on input tags and its operation.

HTML Onclick Events

Example 5

The following examples operate on the click event in the HTML tag with the JavaScript function. We can use the input tag with submit and button type for the script operation. We can use JavaScript code for the onclick event.

Output:

The output shows the onclick event on input tags with JavaScript code.

HTML Onclick Events

Conclusion

The onclick function in HTML is useful for users and developers to validate and notify guidelines of the form. We can change the styles, operation, and javascript function using the onclick event attribute in the html.


Next TopicHTML Photo