Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  17] [ 4]  / answers: 1 / hits: 25360  / 13 Years ago, wed, july 27, 2011, 12:00:00

http://jsfiddle.net/HVGre/1/ - test link.






I have an html link on my page that I need to be able to click dynamically. This works fine with the .click() function in IE, but fails in firefox. I cannot change the link to a button, so that is not an option, it has to be an href.



<a href=javascript:alert('!'); id=myHrefLink>My HREF Link</a>

<script>
document.getElementById(myHrefLink).click();
</script>


Is there a good workaround for this in firefox? I'm able to use jQuery if that opens any possibilities.




  1. I do not intend to assign an event handler to the link, I simply need to click the link dynamically using javascript (without doing so manually with the mouse).


  2. I cannot alter the functionality of the original link. It has to remain as it is.







EDIT:



It seems that the issue Firefox has with this code is that the link does not have an onclick event and has the code referenced via the href, or otherwise NOT onclick (in the example here the code is on href, in my actual code the href is set to just '#', however the link somehow triggers other actions when clicked, don't ask me how, it's a weird flash integration with the plupload tool).



<a href=javascript:alert('This works!');>Click me dynamically</a>


VS



<a href=# onclick=alert('This works!');>Click me dynamically</a>


The second example is solid and works in all browsers when the click() function is triggered, however I need the first of these two to work without changing the link dynamically or otherwise. Any clever ideas?


More From » jquery

 Answers
54

The answer is that you cannot dynamically click on the href portion of a link in firefox or chrome at this time.



<a href=javascript:alert('This works!');>This does not work dynamically.</a>


VS



<a href=# onclick=alert('This works!');>This works dynamically onclick.</a>


It seems that FireFox is only able to fire a click event on an HTML link when an onclick property is present. There are tons of workarounds for this so long as the link can be altered in some way (see most comments in this post), but as far as doing what I originally intended to, that only works in IE, currently.



http://jsfiddle.net/HVGre/1/ - see failed results here.


[#90963] Tuesday, July 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
susanajamiep

Total Points: 466
Total Questions: 113
Total Answers: 108

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
;