The Wayback Machine - https://web.archive.org/web/20240914024756/https://www.geeksforgeeks.org/html-onclick-event-attribute/
Open In App

HTML onclick Event Attribute

Last Updated : 26 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

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

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.

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


Previous Article
Next Article

Similar Reads

How to stop event propagation with inline onclick attribute in JavaScript ?
Use HTML DOM stopPropagation() method to stop the event from propagating with inline onclick attribute which is described below: HTML DOM stopPropagation() Event Method: The stopPropagation() method is used to stop propagation of event calling i.e. the parent event is called we can stop the propagation of calling its children by using the stopPropa
2 min read
HTML DOM onclick Event
The HTML DOM onclick event occurs when the user clicks on an element. There are three ways to add onclick events: Syntax: In HTML: &lt;element onclick="myScript"&gt; In JavaScript: object.onclick = function(){myScript}; In JavaScript, using the addEventListener() Method: object.addEventListener("click", myScript); Example 1: Using HTML C/C++ Code
2 min read
How to Add & Remove Value Onclick Event in HTML ?
To add a value to an element's onclick event in HTML, you can set the event attribute to a JavaScript function or expression, like onclick="myFunction()". To remove it, you can set the attribute to an empty string, like onclick=' '. This allows for dynamic manipulation of event behavior based on user interactions. Using addEventListener methodThis
2 min read
jQuery click() Method vs onClick Event
jQuery is the JavaScript library known for its lightweight, fast, and cross-platform features. It is an open-source library that goes by the motto "Write less, do more". The main objective of jQuery is to make Java script so easy that one can create more attractive and interactive websites. jQuery has multiple events like jQuery onClick(), jQuery c
5 min read
Changing CSS styling with React onClick() Event
Changing CSS styling with React onClick() Event simply means changing the CSS property or CSS classes when the click event triggers. It can be done by switching the states to update the classes when a button is clicked PrerequisitesReact JSReact useState HookApproachChanging CSS styling with React onClick() Event we will use useState variable. We w
2 min read
How to change states with onClick event in ReactJS using functional components ?
In modern web applications, to change states with onClick event in React JS using functional components is done by Hooks in React as the functional components don't have inbuilt states like class components. With Hooks, state objects are completely independent of each other, so you can have as many state objects as you want. In a single-page applic
2 min read
Call multiple JavaScript functions in onclick event
To Call multiple JavaScript functions in onclick event, we have different approaches. In this article, we are going to see how to call multiple JavaScript functions in onClick event. Below are the approaches to calling multiple JavaScript functions in onclick event: Table of Content Calling all functions at onceCalling one function that contains re
2 min read
React onClick Event
React onClick is an event handler utilized to capture and respond to user clicks on specific elements, typically interactive ones like buttons or links. It is similar to the HTML DOM onclick event but uses the camelCase convention in React. React onClick captures the user's click responses and executes the handler functions. React onClick Syntax:on
3 min read
JavaScript onclick Event
The onclick event generally occurs when the user clicks on an element. It's a fundamental event handler in JavaScript, triggering actions or executing functions in response to user interaction, facilitating dynamic and interactive web functionality. In JavaScript, we can use the onclick function in two ways as listed below: Table of Content Applyin
2 min read
What is the purpose of the onClick event in React?
The onClick event plays an important role for enhancing user interactions with components in react applications which allows developers/programmers to call a function or method that gets executed when a particular element is clicked. Whether triggering a state change, navigating to another page, or performing a custom action, the onClick event is a
4 min read
How to change an element color based on value of the color picker value using onclick?
We can define color picker by input type="color". It provides a user interface element that lets a user specify a color, either by using a visual color picker interface or by entering the color into a text field in #rrggbb hexadecimal format. Only colors without alpha channels are allowed. Though CSS colors have more formats, e.g. color names, func
2 min read
How to send state/props to another component in React with onClick?
The props and state are the main concepts of React. Actually, only changes in props and/ or state trigger React to rerender your components and potentially update the DOM in the browser Props: It allows you to pass data from a parent component to a child component. State: While props allow you to pass data from a parent component to a child compone
2 min read
How to Add Onclick Effect using CSS ?
In this article, we will see how to add onclick effect using CSS. To add the onclick effect using CSS, we can use :active pseudo selector. When an element is clicked, the onclick JavaScript event is launched. JavaScript is required to add an event listener to the HTML element and then run some code when the element is clicked in order to produce an
1 min read
JavaScript Pass string parameter in onClick function
In JavaScript, you can pass a string parameter in an onClick function by enclosing the function call in an anonymous function. In this article, we will see how to Pass string parameters in the onClick function. Example 1: This example simply puts the argument which is a string in the onClick attribute of the button which calls a function with a str
2 min read
Difference between addEventListener and onclick in JavaScript
The addEventListener() and onclick both listen for an event. Both can execute a callback function when a button is clicked. However, they are not the same. In this article, we are going to understand the differences between them. addEventListener()The addEventListener() method attaches an event handler to the specified element. Any number of event
3 min read
How to Add List Items Onclick in VueJS ?
In VueJS, we can make dynamic applications by adding list items on click. This allows for interactive user experiences where new items can be added to a list with a click event. The below approaches can be used to accomplish this task. Table of Content By creating a custom method to Add ItemsUsing Input field with v-modelBy creating a custom method
2 min read
How The ReactJS onClick Can't Pass Value To Method?
In React, handling events like onClick is a common task, often requiring passing values to event handlers. However, developers sometimes encounter issues where the onClick event handler can't seem to pass values to the method. Understanding why this happens and how to handle it properly is essential for effective react development. When you attach
3 min read
Uncaught ReferenceError at HTMLButtonElement.onclick
In this article article, let's first discuss the Reference error and then come to onclick function and then we come over the question Uncaught ReferenceError at HTMLButtonElement.onclick using example and lastly come to the solution approach. Onclick Function in javaScript: The onclick event handlers are a group of properties offered by DOM element
2 min read
How to Handle onClick Events on ChartJS Bar Charts in Chart.JS?
In Chart.js, we can add behavior to bar charts by handling onClick events. This allows developers to create interactive charts where specific actions are triggered when a user clicks on a particular bar. Such actions can range from displaying additional information to dynamically updating the chart data, providing a more engaging user experience. C
3 min read
HTML onsearch Event Attribute
This onsearch attribute works when a user presses the ENTER button or clicks on the x button. This event works with &lt;input&gt; element. Supported Tags: &lt;input type="search"&gt;Syntax: &lt;element onsearch="script"&gt;Attribute: This event contains single attribute script which contains the script to be run when event call. Example: C/C++ Code
1 min read
HTML oninput Event Attribute
This attribute works when it gets user input value. This attribute mainly fires when the user change the value of &lt;input&gt; and &lt;textarea&gt; element. This attribute is quite similar to onchange attribute but the basic difference is that the oninput event attribute occurs immediately when the value of the element changes while the onchange a
1 min read
HTML | onmouseup Event Attribute
This attribute fires when a mouse button is released over the element. The order of events occur related to the onmouseup event. onmousedownonmouseuponclick Supported Tags: All HTML elements, EXCEPT: &lt;base&gt; &lt;bdo&gt;&lt;br&gt;&lt;head&gt;&lt;html&gt;&lt;iframe&gt;&lt;meta&gt;&lt;param&gt;&lt;script&gt;&lt;style&gt; &lt;title&gt; Syntax:
1 min read
HTML | onwheel Event Attribute
This attribute works when the wheel of pointer device is rolled up or down over an element. The onwheel attribute also works when the user scrolls or zooms on an element by using mouse or touchpad. Supported Tags: It supports all HTML elements. Syntax: &lt;element onwheel = "script"&gt; Attribute Value: This attribute contains single value script a
1 min read
HTML | oncontextmenu Event Attribute
This attribute works when the user right-clicks on an element to open the context menu.Supported Tags: It supports all HTML elements. Syntax: &lt;element oncontextmenu="script"&gt; Attribute Value: This attribute contains single value script. The script to be run when oncontextmenu attribute called. It is supported by all HTML elements.Note: This a
1 min read
HTML | onfocus Event Attribute
This onfocus attribute works when the element gets focused. This event attribute is mostly used with &lt;input&gt;, &lt;select&gt;, &lt;a&gt; elements. This event attribute is supported by all HTML elements excepts &lt;base&gt;, &lt;bdo&gt;, &lt;br&gt;, &lt;head&gt;, &lt;html&gt;, &lt;iframe&gt;, &lt;meta&gt;, &lt;param&gt;, &lt;script&gt;, &lt;sty
1 min read
HTML | ononline Event Attribute
The ononline event attribute works when the browser starts working in online mode. it is opposite to onoffline event attribute.Supported Tags &lt;body&gt; syntax &lt;element ononline = "script"&gt; Attribute Value: This event attribute contains single value script which works when browser working in online mode.Example: C/C++ Code &lt;!DOCTYPE HTML
1 min read
HTML | ondragover Event Attribute
The ondragover event attribute will trigger if a draggable element or text is being dragged to a valid drop target. To drag an element garb that element and drag it to the drag point. Here we will use the global HTML 5 draggable attribute. The data and elements can't be dropped. To allow a drop, you have to call the event.preventDefault() method. I
2 min read
HTML onended Event Attribute
The HTML onended Attribute is a event attribute which works when the audio/video is ended. We can add some custom message in this event like “Thank for watching”, “Share”, etc. Usage: This attribute is used in &lt;audio&gt; and &lt;video&gt; element. Syntax: &lt;element onended="myScript"&gt;Attribute Value: This attribute contains single value scr
1 min read
HTML onmouseover Event Attribute
The onmouseover event attribute works when the mouse pointer moves over the specified element. It is basically: Activated when the mouse pointer enters the designated HTML element.Enables executing JavaScript code when the mouse hovers over the element.Ideal for enhancing user experience by providing feedback or additional information.Syntax&lt;ele
2 min read
HTML onunload Event Attribute
The onunload event attribute works when the document is being unloaded i.e. it occurs when the browser has been closed. It is mostly used when the user opens a link and submits the form and closes the browser window. Basically, it is: Fired when a user navigates away from a page or closes the browser.Useful for executing cleanup actions or displayi
1 min read