The onclick event attribute works when the user click on the button. When mouse clicked on the element then the script run.
Syntax:
<element onlick = "script">
Attribute Value: This attribute contains single value script which works when mouse clicked on the element.
Supported Tags: This attribute is supported by all HTML tags except <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>.
Example 1:
<!DOCTYPE HTML> < html > < head > < title > onclick event attribute </ title > < script > /* If onclick event call then this script will run */ function onclick_event() { document.getElementById("geeks").innerHTML = "Welcome to GeeksforGeeks!"; } </ script > </ head > < body style = "text-align:center" > <!-- onclick event call here --> < p id = "geeks" onclick = "onclick_event()" > Click Here </ p > </ body > </ html > |
Output:
Before:
Click Here
After:
Welcome to GeeksforGeeks!
Example 2: Use button tag to call the event.
<!DOCTYPE html> < html > < head > < title > onclick event attribute </ title > < script > function funonclick() { document.getElementById("geeks").innerHTML = "Welcome to GeeksforGeeks!"; } </ script > </ head > < body style = "text-align:center" > <!-- onclick event call here --> < button onclick = "funonclick()" > Click Here </ button > < p id = "geeks" ></ p > </ body > </ html > |
Output:
Supported Browser: The browser supported by onclick event attribute are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Safari
- Opera