7 out of 14 rated this helpful - Rate this topic

click method

[This documentation is preliminary and is subject to change.]

Simulates a click by causing the onclick event to fire.

Document Object Model (DOM) Level 2 HTML Specification, Section 1.6.5

Syntax

object.click()

Standards information

Parameters

This method has no parameters.

Return value

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Remarks

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

Examples

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

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/click.htm


<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>

 

 

Build date: 3/8/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Remark
Note that this click() method does not fire mouse-associated events such as onmouseup.

Remedy: Use obj.fireEvent("onmouseup").
The W3C DOM only defines click method for INPUT elements

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()".