The click event is triggered for an element when it is activated by a mouse click or by another user action that normally has the same effect as a mouse click.
This example uses the event object to gain information about the origin of the click. In addition, it cancels the default action to prevent navigation of anchor elements, unless the SHIFT key is pressed. Normally a Shift+Click opens the target of a link in a new window; however, the script replaces the current document by setting the location of the window object.
This example shows how to bind the onclick event to grouped controls.
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 document 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 documents that use the elements accessible to keyboard users. For more information, please see the section on writing accessible Dynamic HTML. Initiates any action associated with the object. For example, if the user clicks an a object, the client loads the document specified by the href property. To cancel the default behavior, set the returnValue property to FALSE. To invoke this event, do one of the following:
- 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.
Internet Explorer 8 & 9
Internet Explorer 8 & 9 suffer from a bug where elements with a computed
transparent that are overlaid on top of other element(s) won’t receive click events. Any click events will be fired at the underlying element(s) instead. See this live example for a demonstration.
Known workarounds for this bug:
- For IE9 only:
- For IE8 and IE9
: alpha(opacity=0);and an explicit
iOS Safari suffers from a bug where click events aren’t fired on elements that aren’t typically interactive (e.g.
div) and which also don’t have event listeners directly attached to the elements themselves (i.e. event delegation is being used). See this live example for a demonstration. Safari considers the following elements to be typically interactive (and thus they aren’t affected by this bug):
label (but it must be associated with a form control),
Known workarounds for this bug:
: pointer;on the element.
- Add a dummy
onclick=""attribute to the element.
- Use a typically interactive element (e.g.
a) instead instead of one that isn’t typically interactive (e.g.
- Stop using click event delegation.
- HTML 4.01 Specification, Section 18.2.3
Event handler parameters
- pEvtObj [in]
- Type: ****IHTMLEventObj****
Microsoft Developer Network: [Windows Internet Explorer API reference Article]