HTML onclick Event Attribute Last Updated : 26 Jun, 2024 Comments Improve Suggest changes 3 Likes Like Report The onclick event attribute in HTML triggers when the user clicks on an element. It executes a script or function upon a click event and is commonly used for interactive elements like buttons and links. Syntax<element onclick = "script">Attribute Value This attribute contains a single value script that works when the mouse clicks on the element. Example 1: Display Date & Time on Button ClickThis example shows a simple onclick event where clicking the button displays the current date and time. HTML <!DOCTYPE html> <html> <head> <title>HTML onclick Event Attribute</title> <style> body { display: flex; justify-content: center; align-items: center; flex-direction: column; font-family: sans-serif; } h2 { color: green; } </style> </head> <body> <h2>GeeksforGeeks</h2> <h3>HTML onClick Event Attribute</h3> <span> Click the button to see the date & time. </span> <button onclick="getElementById('time'). innerHTML= Date()"> Show Date </button> <p id="time"></p> </body> </html> Output: HTML onclick Event Attribute Example OutputExample 2: Display Text on ClickIn this example, when the "Click Here" will be clicked then the text "Welcome to GeeksforGeeks" will be displayed. HTML <!DOCTYPE HTML> <html> <head> <title>HTML onclick event attribute </title> <style> body { display: flex; justify-content: center; align-items: center; flex-direction: column; font-family: sans-serif; } </style> </head> <body> <h3>HTML onClick Event Attribute</h3> <!-- onclick event call here --> <p id="geeks" onclick="onclick_event()"> Click Here </p> <script> /* If onclick event call then this script will run */ function onclick_event() { document.getElementById("geeks").innerHTML = "Welcome to GeeksforGeeks!"; } </script> </body> </html> Output: HTML onclick Event Attribute Example OutputThe onclick event attribute is a powerful tool for adding interactivity to web pages. By attaching scripts to click events on various elements, developers can create dynamic and responsive user interfaces that enhance the user experience. Create Quiz Comment P PERIYANAYAKI Follow 3 Improve P PERIYANAYAKI Follow 3 Improve Article Tags : Technical Scripter Web Technologies HTML Technical Scripter 2018 HTML-Attributes +1 More Explore HTML BasicsHTML Introduction4 min readHTML Editors4 min readHTML Basics7 min readStructure & ElementsHTML Elements4 min readHTML Attributes7 min readHTML Headings3 min readHTML Paragraphs3 min readHTML Text Formatting4 min readHTML Block and Inline Elements3 min readHTML Charsets4 min readListsHTML Lists3 min readHTML Ordered Lists5 min readHTML Unordered Lists4 min readHTML Description Lists3 min readVisuals & MediaHTML Colors11 min readHTML Links Hyperlinks2 min readHTML Images7 min readHTML Favicon4 min readHTML Video4 min readLayouts & DesignsHTML Tables10 min readHTML Iframes4 min readHTML Layout4 min readHTML File Paths3 min readProjects & Advanced TopicsHTML Forms5 min readHTML5 Semantics5 min readHTML URL Encoding4 min readHTML Responsive Web Design11 min readTop 10 Projects For Beginners To Practice HTML and CSS Skills8 min readTutorial ReferencesHTML Tags - A to Z List15+ min readHTML Attributes Complete Reference8 min readHTML Global Attributes5 min readHTML5 Complete Reference8 min readHTML5 MathML Complete Reference3 min readHTML DOM Complete Reference15+ min readHTML DOM Audio/Video Complete Reference2 min readSVG Element Complete Reference5 min readSVG Attribute Complete Reference8 min readSVG Property Complete Reference7 min readHTML Canvas Complete Reference4 min read Like