Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Anchor click() method

47 views
Skip to first unread message

c...@anna-and-dale.com

unread,
Apr 8, 2007, 11:16:30 PM4/8/07
to
I wish to force an anchor element to have its onclick() event fired.
In IE I just call the click() method on the anchor but this doesn't
work in Firefox (http://developer.mozilla.org/en/docs/
DOM:element.click) so is there another way to accomplish this?

Cheers,

Boris Zbarsky

unread,
Apr 8, 2007, 11:30:49 PM4/8/07
to
c...@anna-and-dale.com wrote:
> I wish to force an anchor element to have its onclick() event fired.

You can use DOM2 Events for this. Note that that won't trigger the link
traversal unless you're privileged script, but it _will_ fire onclick handlers.

-Boris

CoolKiwiBloke

unread,
Apr 9, 2007, 12:21:43 AM4/9/07
to

I'm assuming you mean something like this?

var evt = document.createEvent("UIEvents");
evt.initUIEvent("click", true, true, window, 1);
AnchorFieldObj.dispatchEvent(evt);

Unfortunately the onClick event on the Anchor element doesn't fire
with this so I assume I am doing something wrong?

Cheers,

Boris Zbarsky

unread,
Apr 9, 2007, 12:34:56 AM4/9/07
to
CoolKiwiBloke wrote:
> var evt = document.createEvent("UIEvents");

I'd use MouseEvents myself. And initMouseEvent.

-Boris

CoolKiwiBloke

unread,
Apr 9, 2007, 12:51:18 AM4/9/07
to

Hmmm... even using the following code the onclick() event handler
never fires... am I still missing something?

var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false,
false, false, false, 0, null);
AnchorFieldObj.dispatchEvent(evt);

Boris Zbarsky

unread,
Apr 9, 2007, 12:59:10 AM4/9/07
to
CoolKiwiBloke wrote:
> Hmmm... even using the following code the onclick() event handler
> never fires... am I still missing something?
>
> var evt = document.createEvent("MouseEvents");
> evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false,
> false, false, false, 0, null);
> AnchorFieldObj.dispatchEvent(evt);

Offhand that looks like it should work....

-Boris

CoolKiwiBloke

unread,
Apr 9, 2007, 1:22:47 AM4/9/07
to

You're absolulte correct - it is working! I had something else wrong
as well. Thanks for your help - much appreciated!

0 new messages