DHTML Events
Fires when the user clicks the left mouse button on the object.
Inline HTML |
<element onclick = "handler" ... > |
All platforms |
Event property |
object.onclick = handler |
ECMA-262 Language Specification |
Named script |
<script FOR=object EVENT=onclick> |
Internet Explorer® only |
Bubbles |
Yes |
Cancels |
Yes |
To invoke |
- Click the object.
- Invoke the click method.
- Press ENTER in a form.
- Press the access key for a control.
- Select an item in a combo box or list box by clicking the left mouse button or by pressing the arrow keys and then pressing the ENTER key.
|
Default action |
Initiates any action associated with the object. For example, clicking an A object causes the browser to load the document specified by the href property. To cancel the default behavior, set the returnValue property of the event object to FALSE. |
A click event occurs when the pointing device button is clicked over an element. A click is defined as a mousedown and mouseup over the same screen location. The sequence of these events is: mousedown, mouseup, click.
For example, if the user clicks the left mouse button, the onclick event for an object occurs only if the mouse pointer is over the object and both an onmousedown and an onmouseup event occur in order. if the user presses down in the object but moves the mouse pointer out of the object before releasing, no onclick event occurs.
The onclick event changes the value of a control in a group. This change initiates the event for the group, not for the individual control. For example, if the user clicks a radio button or check box in a group, the onclick event occurs after the onbeforeupdate and onafterupdate events for the control group.
If the user clicks an object that can receive the input focus but does not already have the focus, the onfocus event occurs for that object before the onclick event. If the user double-clicks the left mouse button in a control, an ondblclick event occurs immediately after the onclick event.
Although the onclick event is available on a large number of HTML elements, if a Web page is to be accesible to keyboard users, its use should be restricted to the A, INPUT, AREA, and BUTTON elements. These elements automatically allow keyboard access through the TAB key, making Web pages that use them accessible to keyboard users.
While event handlers in the Document Object Model do not receive parameters directly, the handler can query the event object for data.
Event Object Properties
altKey |
Retrieves the current state of the ALT key. |
cancelBubble |
Sets or retrieves whether the current event should bubble up the hierarchy of event handlers. |
clientX |
Retrieves the x-coordinate of the position of the cursor when the mouse is clicked, relative to the size of the client area of the window but excluding window decorations or scroll bars. |
clientX |
Returns the y-coordinate of the position of the cursor when the mouse is clicked, relative to the size of the client area of the window but excluding window decorations or scroll bars. |
ctrlKey |
Retrieves the state of the CTRL key. |
offsetX |
Retrieves the horizontal coordinate of the mouse's position relative to the object firing the event. |
offsetX |
Retrieves the vertical coordinate of the mouse's position relative to the object firing the event. |
returnValue |
Sets or retrieves the return value from the event. |
screenX |
Retrieves the horizontal position of the mouse, in pixels, relative to the user's screen. |
screenY |
Retrieves the vertical position of the mouse, in pixels, relative to the user's screen. |
shiftKey |
Retrieves the state of the SHIFT key. |
srcElement |
Retrieves the object that fired the event. |
type |
Retrieves the event name from the event object. |
x |
Returns the horizontal position of the mouse when the event fires. |
y |
Returns the vertical position of the mouse when the event fires. |
The following examples demonstrate the use of onclick event handlers for a document.
This example uses the event object to gain information about the origin of the click, and cancels the default action if the onclick event is fired off an anchor.
<head>
<script language="JavaScript">
function clickIt ( ) {
txtOutput.value = window.event.srcElement.tagName;
txtOutput1.value = window.event.srcElement.type;
// checks if the click occurred in an anchor, then cancels the event
if (window.event.srcElement.tagName=="A"){
window.event.returnValue = false;}}
</script>
</head>
<body onclick="clickIt()">
<div align="center">
<input value="Click Here" size=10>
<span style="background:navy;color:lime">
Click Here </span>
<input type=button value="Click Here">
<a href="#top">Click Here</a>
<br><br>
What's been clicked<br>
<input name=txtOutput></input>
<input name=txtOutput1></input></div>
</body>
Show me
This example shows how to bind the onclick event to grouped controls.
<head>
. . .
<script language="JavaScript">
function getOpt ( ) {
txtOutput.value = window.event.srcElement.value;
}
</script>
</head>
<body>
. . .
<div align=center>
<table>
<tr>
<td><input type=radio name=optGroup id=sex
value="Sex" onclick="getOpt()"></td>
<td><label for=sex>Sex</label></td></tr>
<tr>
<td><input type=radio name=optGroup id=drugs
value="Drugs" onclick="getOpt()"></td>
<td><label for=drugs>Drugs</label></td></tr>
<tr>
<td><input type=radio name=optGroup id=r&r
value="Rock and Roll" onclick="getOpt()"></td>
<td><label for=r&r>Rock and Roll</label></td></tr>
</table>
<p>Value of control on which the
<b>onclick</b> event has fired.</p>
<input name=txtOutput size=10></input></div>
</body>
Show me
click, ondblclick