onclick (HTML Attribute)

Ophelie Lechat
Ophelie Lechat
Share

Description

The onclick event handler captures a click event from the users’ mouse button on the element to which the onclick attribute is applied. This action usually results in a call to a script method such as a JavaScript function, like this:
onclick="displayHelpInfo();"
However, it can also be used to run a script in situ:
onclick="alert('You are clicking on me');"

Note that this event attribute cannot be applied to the following elements:

  • applet
  • base
  • basefont
  • bdo
  • br
  • font
  • frame
  • frameset
  • head
  • html
  • iframe
  • isindex
  • meta
  • param
  • script
  • style
  • title

Example

Clicking anywhere on the div below will call a function, defined elsewhere, called showStats():

<div onclick="showStats();">Figures for February’s racing.</div>

Value

This attribute has no fixed value. It’s up to the author to decide on the scripting that’s included here, be that a call to one or more defined functions, or a simple alert() statement.

However, the likely values will be similar to this:

onclick="doMyFunction();"

You could also specify a value like this:

onclick="doThisFunction();thenDoTheOtherFunction();"

You may also use a value like this:

onclick="alert('Hello world');window.close();"

Note that you can string several functions together, separating them with a semicolon, as shown in the second and third examples above.

Compatibility

Internet Explorer Firefox Safari Opera
5.5 6.0 7.0 1.0 1.5 2.0 1.3 2.0 3.0 9.2 9.5
Full Full Full Full Full Full Full Full Full Full Full

Every browser listed supports this attribute. However, inline event handlers such as this should be avoided. In the same way that inline CSS styles are frowned upon but externally defined CSS styles are considered good practice, inline event handlers should be stripped out and replaced with events attached unobtrusively through the DOM.

Frequently Asked Questions (FAQs) about the onClick HTML Attribute

What is the onClick HTML attribute and how does it work?

The onClick HTML attribute is a JavaScript event attribute that is triggered when a user clicks on a specific HTML element. It is commonly used to execute a script when a button is clicked. The script can be written directly within the onClick attribute or called from an external JavaScript file. The onClick attribute is versatile and can be used with various HTML elements, including buttons, images, and links.

How can I use the onClick attribute with a button?

To use the onClick attribute with a button, you simply need to include the attribute within the button element. For example, <button onClick="myFunction()">Click me</button>. In this case, when the button is clicked, it will call the function named “myFunction”.

Can I use the onClick attribute with other HTML elements?

Yes, the onClick attribute can be used with almost any HTML element, not just buttons. For example, you can use it with an image to trigger a script when the image is clicked. The syntax would be similar to this: <img src="image.jpg" onClick="myFunction()">.

How can I use the onClick attribute to redirect to another webpage?

You can use the onClick attribute to redirect to another webpage by using the window.location method within the onClick attribute. For example, <button onClick="window.location.href='https://www.example.com'">Go to Example.com</button>. When this button is clicked, it will redirect the user to www.example.com.

Can I use multiple onClick events on a single element?

No, you cannot use multiple onClick events on a single element. However, you can call multiple functions within a single onClick event. For example, <button onClick="function1(); function2();">Click me</button>. When this button is clicked, it will execute both function1 and function2.

How can I pass parameters to a function using the onClick attribute?

You can pass parameters to a function using the onClick attribute by including the parameters within the parentheses of the function call. For example, <button onClick="myFunction('Hello', 'World')">Click me</button>. In this case, when the button is clicked, it will call the function named “myFunction” and pass ‘Hello’ and ‘World’ as parameters.

Can I use the onClick attribute with form elements?

Yes, you can use the onClick attribute with form elements. For example, you can use it with a submit button to validate form data before it is submitted. The syntax would be similar to this: <input type="submit" onClick="validateForm()">.

How can I prevent the default action of an element using the onClick attribute?

You can prevent the default action of an element using the onClick attribute by using the event.preventDefault() method within the onClick attribute. For example, <a href="https://www.example.com" onClick="event.preventDefault(); myFunction();">Click me</a>. In this case, when the link is clicked, it will not redirect to www.example.com, but instead, it will call the function named “myFunction”.

Can I use the onClick attribute with SVG elements?

Yes, you can use the onClick attribute with SVG elements. For example, you can use it with a circle element to change its color when it is clicked. The syntax would be similar to this: <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" onClick="changeColor(this)">.

How can I use the onClick attribute to toggle between two functions?

You can use the onClick attribute to toggle between two functions by using a boolean variable to keep track of the state. For example, <button onClick="toggleFunction()">Click me</button>. In this case, the function named “toggleFunction” would check the state of the boolean variable and call the appropriate function based on its value.