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:
In HTML:
In JavaScript:
<ELEMENT onclick="handler"> |
In JavaScript:
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.
For a complete list of events, see the page for Events in JavaScript. |
Basic information:
Bubbles | Yes |
Cancelable | Yes |
Event object | MouseEvent |
Actions that invoke the onclick event:
The order of events related to the onclick event:
Action | Event order |
---|---|
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:
|
||||
<head> <script type="text/javascript"> function OnClickButton () { alert ("You clicked on the button!"); } </script> </head> <body> <button onclick="OnClickButton ()">Click on me!</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example shows how to detect when the checked state of a input:checkbox element is changed:
|
||||
<head> <script type="text/javascript"> function OnChangeCheckbox (checkbox) { if (checkbox.checked) { alert ("The check box is checked."); } else { alert ("The check box is not checked."); } } </script> </head> <body> Toggle the checked state of the following checked box:<br /><br /> <input type="checkbox" onclick="OnChangeCheckbox (this)" id="myCheckbox" /> <label for="myCheckbox">Sample check box</label> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 3:
This example shows how to detect when the selection has changed in a input:radio group:
|
||||
<head> <script type="text/javascript"> function OnChangeRadio (radio) { alert ("The " + radio.value + " radio is selected."); } </script> </head> <body> Select your sex:<br /><br /> <input type="radio" name="sex" id="radio_female" value="female" onclick="OnChangeRadio (this)" /> <label for="radio_female">female</label> <br /> <input type="radio" name="sex" id="radio_male" value="male" onclick="OnChangeRadio (this)" /> <label for="radio_male">male</label> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
document, window
HTML elements:
a, abbr, acronym, address, applet, area, b, bdo, big, blink, blockquote, body, button, caption, center, cite, code, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, h1, h2, h3, h4, h5, h6, hr, html, i, img, input:button, input:checkbox, input:file, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, keygen, label, legend, li, listing, map, marquee, menu, nobr, noframes, object, ol, optgroup, option, p, plaintext, pre, q, rt, ruby, s, samp, select, small, span, strike, strong, sub, sup, table, tbody, td, textarea, tfoot, th, thead, tr, tt, u, ul, var, xmp
Related pages:
External links:
User Contributed Comments