Horje
HTML onclick Attribute

Definition and Usage

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


Applies to

The onclick attribute is part of the Event Attributes, and can be used on any HTML elements.

Elements Event
All HTML elements onclick

Browser Support


How to Execute a JavaScript when a button is clicked

Button Example.
index.html
Example: HTML
<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>

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

P Example.
index.html
Example: HTML
<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>

Output should be:

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




html onclick attribute

Type:
Develop
Category:
Web Tutorial
Sub Category:
HTML Attribute
Uploaded by:
Admin


Read Article
https://develop.horje.com/learn/1434/reference