PDA

View Full Version : element.click() not working in FF


wac
03-09-2006, 09:45 PM
From the gecko documentation
http://developer.mozilla.org/en/docs/DOM:element.click

there should be a click method on element. But I get errors
Error: elem.click is not a function

when I try to use it. I saw an earlier thread about how to add a function prototype to all elements so that I can add my own click method.
I'm just wondering why it doesn't work, or perhaps I'm doing something wrong...
I'm using FF 1.5.0.1

DSB
03-09-2006, 10:01 PM
What is your exact syntax?

Beagle
03-09-2006, 10:05 PM
Are you trying to fire a click event manually? or are you trying to execute something when that element is clicked?

The latter is accomplished with element.onclick= function(){alert('Hi');};

wac
03-09-2006, 10:05 PM
I have an anchor on my test page
<a id='myclick' href='javascript:alert("you clicked me")'>Click Me</a>

and a javascript function
function tryclick()
{
var elem = document.getElementById("myclick") ;
elem.click() ;
}

DSB
03-09-2006, 10:08 PM
Why not just do:


<a onClick="tryclick()" href='javascript:alert("you clicked me")'>Click Me</a>


If there is something else you are trying to do let us know.

wac
03-09-2006, 10:09 PM
I know about onclick, but the anchor which exists has an href and no onclick function. I can't change that code. The href is a url, unlike my example.
I need to simulate a click on the anchor when a particular key is pressed, so I've got a keypress handler and I can find the anchor, now I need to get the click to occur. Note that I can't modify the existing page, but I can add to it...

wac
03-09-2006, 10:10 PM
The anchor is generated by some funky code in a JSP. I can't modify the anchor.

Beagle
03-09-2006, 10:13 PM
Ok, you're trying to fire the click event manually.

Have you looked into "access keys"? Check google.

(this is the first link)
http://www.alistapart.com/articles/accesskeys/

wac
03-09-2006, 10:18 PM
I know about accesskey but I cannot modify the original anchor code.
I can add javascript to the page but I cannot change the original HTML.

I know that I could do something like document.getElementById('myanchor').accesskey = 'x' ;
but accesskey moves the focus to the anchor. The user would have to press enter to do the click. In addition, the anchor is hidden in a DHTML popup menu, so it would not receive focus. Which brings up another question...

If an object is not visible (display=none), will its click method work???
I may have to try to do this another way...

jkd
03-09-2006, 10:26 PM
http://www.codingforums.com/showpost.php?p=34595&postcount=5

Beagle
03-09-2006, 10:28 PM
http://www.mozilla.org/docs/dom/domref/dom_el_ref36.html

This is the function I've used for FF

http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/fireevent.asp

And this is the one for IE

Perhaps these will help you.

And I THINK if the event fires, even if it's display='none', it will still trigger standard behavior. Not sure on that one though.

wac
03-09-2006, 10:33 PM
Thanx,
That will probably work better than creating a click prototype on HTMLElement
which seems like overkill (but I'll keep it in my arsenal of Javascript tricks).

Thanx to all of you that responded...

Kor
03-10-2006, 10:18 AM
You may use this trick as well:

function tryclick()
{
var elem = document.getElementById("myclick") ;
location.href=elem.href ;
}