Tutorials References Exercises Videos NEW Menu
Paid Courses Website NEW

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

Examples

Button Example

Execute a JavaScript when a button is clicked:

<button onclick="myFunction()">Click me</button>
Try it Yourself »

P Example

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

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

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

Browser Support

Event Attribute
onclick Yes Yes Yes Yes Yes