VOOZH about

URL: https://www.geeksforgeeks.org/html/html-onclick-event-attribute/

⇱ HTML onclick Event Attribute - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

HTML onclick Event Attribute

Last Updated : 26 Jun, 2024

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 Click

This example shows a simple onclick event where clicking the button displays the current date and time.

Output:

👁 HTML-onclick
HTML onclick Event Attribute Example Output

Example 2: Display Text on Click

In this example, when the "Click Here" will be clicked then the text "Welcome to GeeksforGeeks" will be displayed.

Output:

👁 HTML-onclick-2
HTML onclick Event Attribute Example Output

The 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.

Comment