html tutorial - onclick Attribute in HTML - html5 - html code - html form

Learn html - html tutorial - onclick Attribute in html - html examples - html programs
- The onclick attribute trigger an event on a mouse click on the element.
- The onclick attribute is a section of the Event Attributes.
Syntax for onclick attribute in HTML:
<element onclick="script">
Applies To:
Element | Attribute |
---|---|
All HTML Elements | onclick |
Attribute Values:
Value | Description |
---|---|
script | The script to be run on onclick. |
Sample Coding for onclick Attribute in HTML:
Tryit<!DOCTYPE html>
<html>
<head>
<title>Wikitechy onclick attribute</title>
</head>
<body>
<p onclick="onClick()">Click Here!</p>
<script>
function onClick()
{
alert('Welcome to Wikitechy!');
}
</script>
</body>
</html>
Code Explanation for onclick Attribute in HTML:

- The onclick attribute is used to call JavaScript function “onClick()” when the user click over “Click Here!” .
- onClick() JavaScript function is used to display an alert box.
Output for onclick Attribute in HTML:

- The content Click Here! has been displayed when the page loaded.
- When the user click over the text “Click Here!” alert box will be displayed on the screen.

Browser Support for onclick Attribute in HTML:
![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|
Yes | Yes | Yes | Yes | Yes |