Contents Index Topic Contents | ||
Previous Topic: onchange Next Topic: ondataavailable |
onclick
Description
Fires when the user presses and releases the left mouse button, or when the user presses keys, such as ENTER and ESC, in a form.
Remarks
The following actions cause an onclick event.
- Click the left mouse button.
- Press ENTER when either the submit control or a nonbutton control in the form has focus.
- Press the ESC key when any control in the form has the focus.
- Press the SPACEBAR when a checkbox, radio, reset, or submit control has focus or a BUTTON or TEXTAREA element has focus.
- Press the access key specified by the accessKey property for a checkbox, radio, reset, or submit control or a BUTTON, OPTION, SELECT, or TEXTAREA element.
The default action of an onclick event depends on the object that receives the click. For example, clicking an A element causes the browser to load the document specified by the href property. You can cancel the default behavior by setting 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 both an onmousedown and an onmouseup event occur in order. For example, if the user presses down in the object but moves the mouse pointer out of the object before releasing, no onclick event occurs.
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 tags, its use should be restricted to the <A>, <INPUT>, <AREA>, and <BUTTON> tags. These elements automatically allow keyboard access via the TAB key, making Web pages that use them accessible to keyboard users. For more information, please see the section on Writing Accessible Dynamic HTML.
This event will bubble. Events that bubble can be handled on any parent element of the object that fired the event.
Examples
The following JScript example is an onclick event handler for the document. It uses the event object to display the tag name of the element in which the click occurred.
<SCRIPT> function clickit() { alert("Clicked in " + window.event.srcElement.tagName); } </SCRIPT> <BODY onclick="clickit()">The following VBScript example is an onclick event handler for the document. It uses the event object to determine whether the click occurred in an A element and then cancels the event (and prevents a jump) if the SHIFT key is down.
<SCRIPT LANGUAGE="VBScript"> Function document_onclick() If window.event.srcElement.tagName = "A" And window.event.shiftKey Then window.event.returnValue = False End If End Function </SCRIPT>Applies To
A, ADDRESS, APPLET, AREA, B, BIG, BLOCKQUOTE, BODY, BUTTON, CAPTION, CENTER, CITE, CODE, DD, DFN, DIR, DIV, DL, DT, EM, EMBED, FIELDSET FONT, FORM, H1, H2, H3, H4, H5, H6, HR, I, IMG, INPUT, KBD, LABEL, LEGEND, LI, LISTING, MAP, MARQUEE, MENU, OBJECT, OL, OPTION, P, PLAINTEXT, PRE, S, SAMP, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TR, TT, U, UL, VAR, XMP, document
See Also
Top of Page
© 1997 Microsoft Corporation. All rights reserved. Terms of Use.