Execute a JavaScript when a button is clicked:
Click the button to trigger a function that will output "Hello World" in a p element with id="demo".
The onclick event is used to trigger a function when an element is clicked on.
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Click me</button> <p id="demo"></p> <script> function myFunction() {/*from w w w .j a va 2 s . c o m*/ document.getElementById("demo").innerHTML = "Hello World"; } </script> </body> </html>
The onclick event occurs when the user clicks on an element.
Bubbles: Cancelable: Event type: Supported HTML tags: | Yes Yes MouseEvent All HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title> |
---|