Horje
What is HTML onclick Event Attribute
The onclick event attribute in HTML is used to execute a script, typically a JavaScript function, when a user clicks on an HTML element. It is a fundamental part of creating interactive web pages, allowing developers to define actions that occur in response to user clicks.
 

Example of HTML onclick Event Attribute

It will Execute a JavaScript when a button is clicked.
index.html
Example: HTML
 <button onclick="myFunction()">Click me</button> 

Output should be:

Example of HTML onclick Event Attribute

Definition and Usage of Definition and Usage

The onclick attribute fires on a mouse click on the element.

Browser Support of HTML onclick Event Attribute

Browser Support of HTML onclick Event Attribute

Syntax of HTML onclick Event Attribute

<element onclick="script">

Attribute Values of HTML onclick Event Attribute

Value Description
script The script to be run on onclick

Technical Details of HTML onclick Event Attribute

Supported HTML tags: All HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>

How to Execute a JavaScript when a button is clicked

A function is triggered when the button is clicked. The function outputs some text in a p element with id="demo".

index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<p>A function is triggered when the button is clicked. The function outputs some text in a p element with id="demo".</p>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "Hello World";
}
</script>

</body>
</html>

Output should be:

How to Execute a JavaScript when a button is clicked

How to Click on a <p> element to change its text color to red

Click me to change my text color.

A function is triggered when the p element is clicked. The function sets the color of the p element to red.

index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<p id="demo" onclick="myFunction()">Click me to change my text color.</p>

<p>A function is triggered when the p element is clicked. The function sets the color of the p element to red.</p>

<script>
function myFunction() {
  document.getElementById("demo").style.color = "red";
}
</script>

</body>
</html>

Output should be:

How to Click on a <p> element to change its text color to red

How to Click on a button to copy some text from an input field to another input field

A function is triggered when the button is clicked. The function copies the text from Field1 into Field2.

index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

Field1: <input type="text" id="field1" value="Hello World!"><br>
Field2: <input type="text" id="field2"><br><br>

<button onclick="myFunction()">Copy Text</button>

<p>A function is triggered when the button is clicked. The function copies the text from Field1 into Field2.</p>

<script>
function myFunction() {
  document.getElementById("field2").value = document.getElementById("field1").value;
}
</script>

</body>
</html>

Output should be:

How to Click on a button to copy some text from an input field to another input field




html browsers support

Related Articles
List of Mouse Events Attribute Mouse Events Attribute
What is HTML onclick Event Attribute Mouse Events Attribute
What is HTML ondblclick Event Attribute Mouse Events Attribute
What is HTML onmousedown Event Attribute Mouse Events Attribute
What is HTML onmousemove Event Attribute Mouse Events Attribute

Single Articles
Example of HTML onclick Event AttributeMouse Events Attribute
Definition and Usage of Definition and UsageMouse Events Attribute
Browser Support of HTML onclick Event AttributeMouse Events Attribute
Syntax of HTML onclick Event AttributeMouse Events Attribute
Attribute Values of HTML onclick Event AttributeMouse Events Attribute
Technical Details of HTML onclick Event AttributeMouse Events Attribute
How to Execute a JavaScript when a button is clickedMouse Events Attribute
How to Click on a <p> element to change its text color to redMouse Events Attribute
How to Click on a button to copy some text from an input field to another input fieldMouse Events Attribute

Read Full:
Mouse Events Attribute
Type:
Develop
Category:
Web Tutorial
Sub Category:
Mouse Events Attribute
Uploaded by:
Admin
Views:
17


Reffered: https://www.w3schools.com/tags/ev_onclick.asp