onclick Event | Internet Development Index |
Fires when the user clicks the left mouse button on the object.
Syntax
Inline HTML <ELEMENT onclick = "handler" ... > All platforms Event property object.onclick = handler JScript only object.onclick = GetRef("handler") Visual Basic Scripting Edition (VBScript) 5.0 or later only Named script <SCRIPT FOR = object EVENT = onclick> Internet Explorer only
Event Information
Bubbles Yes Cancels Yes 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.
Event Object Properties
Although event handlers in the DHTML Object Model do not receive parameters directly, a handler can query an event object for data.
altKey Sets or retrieves a value that indicates the state of the ALT key. altLeft Sets or retrieves a value that indicates the state of the left ALT key. cancelBubble Sets or retrieves whether the current event should bubble up the hierarchy of event handlers. clientX Sets or retrieves the x-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars. clientY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars. ctrlKey Sets or retrieves the state of the CTRL key. ctrlLeft Sets or retrieves the state of the left CTRL key. offsetX Sets or retrieves the x-coordinate of the mouse pointer's position relative to the object firing the event. offsetY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the object firing the event. returnValue Sets or retrieves the return value from the event. screenX Retrieves the x-coordinate of the mouse pointer's position relative to the user's screen. screenY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the user's screen. shiftKey Sets or retrieves the state of the SHIFT key. shiftLeft Retrieves the state of the left SHIFT key. srcElement Sets or retrieves the object that fired the event. type Sets or retrieves the event name from the event object. x Sets or retrieves the x-coordinate, in pixels, of the mouse pointer's position relative to a relatively positioned parent element. y Sets or retrieves the y-coordinate, in pixels, of the mouse pointer's position relative to a relatively positioned parent element.
Remarks
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.
Examples
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 Microsoft® Internet Explorer 4.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.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>
Standards Information
This event is defined in HTML 4.0 .
Applies To
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