onclick event | click event
Occurs when the user clicks on an element.
The default actions for the onclick event are different depending on the type of the element on which the event has occurred. For example, if an onclick event occurs on an input:checkbox or input:radio element, the browser changes the checked state of the element. If an onclick event occurs on an anchor element, the browser loads the document specified by the anchor element's href property.
The onclick event is cancelable, if you cancel it, the default action is not performed.The onclick event does not fire for right mouse clicks as well. If you want to detect when the user presses or releases a mouse button over an element, use the onmousedown and onmouseup events.
The onclick event is fired twice for a double click in Firefox, Opera, Google Chrome and Safari and only once in Internet Explorer. If you want to detect when the user double clicks on an element, use the ondblclick event instead.
How to register:
|object.onclick = handler;|
|object.addEventListener ("click", handler, useCapture);|
|object.attachEvent ("onclick", handler);|
You can find the related objects in the Supported by objects section below.
The event object is accessible to all event handlers in all browsers. The properties of the event object contain additional information about the current event. To get further details about these properties and the possible event handler registration methods, please see the page for the event object.
Actions that invoke the onclick event:
The order of events related to the onclick event:
|Pressing and releasing the left mouse button over an element.|
|Pressing an access key.||
|If the user press the ENTER key.|
|If the user press the SPACE key.|
Example HTML code 1:
This example illustrates the use of the onclick event:
Example HTML code 2:
This example shows how to detect when the checked state of a input:checkbox element is changed:
Example HTML code 3:
This example shows how to detect when the selection has changed in a input:radio group:
User Contributed Comments