HTML onclick Event Attribute

Description

The event attribute onclick allows you to set the script to fire when you click on an element.

Syntax

<element onclick="script">

Values

  • scriptThe name of the script to use when the event has been triggered.

Example

<!DOCTYPE html> 
<html> 
  <head>
    <title>Event attribute onclick</title> 
  </head> 
  <body> 
     <p id="demo">Click on the button to run the script:</p> 
     <button onclick="mouseTest1()">Click me!</button> 
<script> 
function mouseTest1 () { 
  var x = document.getElementById ("demo");
  x.style.fontSize = "25px";
  x.style.color = "orange";
  x.style.backgroundColor = "#555555"; 
}
</script> 
   </body> 
</html>

Supported Tags

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

Browser Support

Desktop
Explorer Edge Chrome Firefox Opera Safari
YesYesYesYesYesYes
Tablets / Mobile
Android Chrome Firefox Opera Safari Samsung
YesYesYesYesYesYes