Horje

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



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


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

Type:
Html
Category:
Web Tutorial
Sub Category:
Mouse Events Attribute
Uploaded by:
Admin