View Full Version : element.click() not working in FF
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
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');};
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() ;
}
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.
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...
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/
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...
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.
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...
You may use this trick as well:
function tryclick()
{
var elem = document.getElementById("myclick") ;
location.href=elem.href ;
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.