Click to return to the DHTML, HTML     
onchange Event     oncontextmenu Event     DHTML Events    
Web Workshop  |  DHTML, HTML & CSS

onclick Event


Fires when the user clicks the left mouse button on the object.

Syntax

Inline HTML<ELEMENT onclick = "handler" ... >All platforms
Event propertyobject.onclick = handlerJScript (compatible with ECMA 262 language specification) only
Named script<SCRIPT FOR = object EVENT = onclick>Internet Explorer only

Remarks

BubblesYes
CancelsYes
To invoke
  • Click the object.
  • Invoke the click method.
  • Press the ENTER key 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, if the user clicks an A object, the browser loads the document specified by the href property. To cancel the default behavior, set the returnValue property of the event object to FALSE.

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 an onmousedown and an onmouseup event occur in that order. For example, if the user clicks the mouse on the object but moves the mouse pointer away from 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 accessible to keyboard users, you should restrict its use to the A, INPUT, AREA, and BUTTON elements. These elements automatically allow keyboard access through the TAB key, making Web pages that use the elements accessible to keyboard users. For more information, please see the section on writing accessible Dynamic HTML.

Event Object Properties

Although event handlers in the DHTML Object Model do not receive parameters directly, the handler can query the event object for data.

Event Object Properties

altKeyRetrieves the current state of the ALT key.
cancelBubbleSets or retrieves whether the current event should bubble up the hierarchy of event handlers.
clientXRetrieves the x-coordinate of the mouse cursor relative to the client area of the window, excluding window decorations or scroll bars.
clientYRetrieves the y-coordinate of the mouse cursor relative to the client area of the window, excluding window decorations or scroll bars.
ctrlKeyRetrieves the state of the CTRL key.
offsetXRetrieves the horizontal coordinate of the mouse's position relative to the object firing the event.
offsetYRetrieves the vertical coordinate of the mouse's position relative to the object firing the event.
returnValueSets or retrieves the return value from the event.
screenXRetrieves the horizontal position of the mouse, in pixels, relative to the user's screen.
screenYRetrieves the vertical position of the mouse, in pixels, relative to the user's screen.
shiftKeyRetrieves the state of the SHIFT key.
srcElementRetrieves the object that fired the event.
typeRetrieves the event name from the event object.
xRetrieves the x-coordinate of the mouse cursor relative to the parent element.
yRetrieves the y-coordinate of the mouse cursor relative to the parent element.

Example

The following examples show how to use the onclick event handlers for the document.

Sample Code

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 while the SHIFT key is being pressed.

<SCRIPT LANGUAGE="JScript">
/* This code determines whether the click occurred in an anchor
   and then cancels the event, preventing the jump, if the SHIFT 
   key is down. */
function clickIt()  
{
    txtOutput.value = window.event.srcElement.tagName;
    txtOutput1.value = window.event.srcElement.type;

    if ((window.event.srcElement.tagName) && ("A" + 
        window.event.shiftKey))
        window.event.returnValue = false;  
}
</SCRIPT>

<BODY onclick="clickIt()" TOPMARGIN="0" LEFTMARGIN="0">

This feature requires Internet Explorer 4.0 or later. Click the icon below to install the latest version. Then reload this page to view the sample.
Microsoft Internet Explorer

This example shows how to bind the onclick event to grouped controls.

<HEAD>
<SCRIPT LANGUAGE="JScript">

function CookieGroup() 
{
txtOutput.value = window.event.srcElement.value;
}
</SCRIPT>
</HEAD>
<BODY>
<!-- Controls are grouped by giving them the same NAME but unique IDs. -->
<P>Grouped Radio Buttons<BR>
<INPUT TYPE=radio NAME=rdoTest ID=Cookies VALUE="accept_cookies" CHECKED
       onclick="CookieGroup()"><BR>
<INPUT TYPE=radio NAME=rdoTest ID=NoCookies VALUE="refuse_cookies"
       onclick="CookieGroup()"><BR>
<P>Ungrouped Radio Button<BR>
<INPUT TYPE=radio NAME=rdoTest1 VALUE="chocolate-chip_cookies"
       onclick="CookieGroup()"><BR>
<P>Value of control on which the onclick event has fired<BR>
<TEXTAREA NAME=txtOutput STYLE="width:250"></TEXTAREA>
</BODY>

This feature requires Internet Explorer 4.0 or later. Click the icon below to install the latest version. Then reload this page to view the sample.
Microsoft Internet Explorer

Applies To

[ Object Name ]
PlatformVersion
Win16:
Win32:
Mac:
Unix:
WinCE:
A, ADDRESS, APPLET, AREA, B, BDO, BIG, BLOCKQUOTE, BODY, BUTTON, CAPTION, CENTER, CITE, CODE, custom, DD, DFN, DIR, DIV, DL, document, DT, EM, EMBED, FIELDSET, FONT, FORM, Hn, HR, I, IMG, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, KBD, LABEL, LEGEND, LI, LISTING, MAP, MARQUEE, MENU, NEXTID, NOBR, OBJECT, OL, P, PLAINTEXT, PRE, RT, RUBY, S, SAMP, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TR, TT, U, UL, VAR, XMP

See Also

click



Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.