click Method Home
click Method

Simulates a click by causing the onclick event to fire.

Syntax

object.click()

Return Value

No return value.

Remarks

Note  Simulating a click using the click does not bring the element being clicked into focus. (See example below).

Example

The following example demonstrates how simulating a click using the click does not, by default, bring the element into focus.

<HTML>
<HEAD>
<SCRIPT>
function simclick1()
{
chk1.focus(); //focus is explicitly set
chk1.click();
}
function simclick2()
{
chk1.click();
}
</SCRIPT>
<SCRIPT FOR=chk1 EVENT=onfocus>
alert("check box is in focus!");
</SCRIPT>
</HEAD>
<BODY>
<P STYLE="font-family:sans-serif;font-weight:bold">DEMO: USING CLICK METHOD 
DOES NOT SET FOCUS<P>
<UL STYLE="color:blue;font-family:sans-serif;font-weight:bold">
<LI>Both these buttons apply the click method to the check box. </LI>
<LI>An alert has been set to fire when the check box is put into focus. 
</UL>
</PLI>
<INPUT Type="CHECKBOX" id=chk1></INPUT>
<br>
<BUTTON onclick="simclick1()">This button <B>applies the focus method</B> to 
check box</BUTTON>
<br><BUTTON onclick="simclick2()">This button <B>does not apply the focus 
method</B> to check box</BUTTON>
<br>
</BODY>
</HTML>
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.

Standards Information

This method is defined in World Wide Web Consortium (W3C) Document Object Model (DOM) Level 1 World Wide Web link.

Applies To

A, ADDRESS, APPLET, AREA, B, BIG, BLOCKQUOTE, BODY, BUTTON, CAPTION, CENTER, CITE, CODE, CUSTOM, DD, DFN, DIR, DIV, DL, 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, NOBR, 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, Element Constructor
Community Content

The W3C DOM only defines click method for INPUT elements
Added by:Daniel B·

In browsers that primarily only implement the W3C DOM functionality, such as Firefox and Sarafi, the click method is only available for INPUT elements of type "Button", "Checkbox", "Radio", "Reset", or "Submit", i.e. not links (A elements) etc.

So, if you need to simulate a click of other elements, such as a link, you'll have to do this in an alternative way for browsers other than IE.

As described by Achernar on webmastersworld.com, for links, you could do the following, where you have previously retrieved a rererence to the link using getElementById etc:

if (!link.onclick ¦¦ link.onclick()) location = link.href;


This links to the location specified by the link's href where there is no onclick event handler or the onclick handler returns true.

This also works fine where the href is script, e.g. "javascript: SubmitForm()".

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View