Register  |  Login



Earn money by sharing your knowledge through Quomon's revenue sharing program

Question

Status: Closed Points: 125 Time: 05:47 - Jul 09, 2007  

dustPuppy

simulation a click on an html anchor with javascript?

I need to simulate a click on an anchor link through javascript. But it only works in IE 7, not in FF 2.
Basically I need to capture an Enter-click on a textbox and call the right button to execute afterwards.
I've added the following to the onkeydown attribute on the textfield:
if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('searchButton').click();return false;}} else {return true}; "

The "searchButton" is an html anchor (<a href=...>) and in IE7 the "click" is performed without problems. In firefox it says that ....click is not a function.
So I'm a bit confused here. Anybody know what is the problem?

Answer Discussion
Tutorials

 

Q&A System for Websites and Corporate Collaboration

Advertisement

  • Generates significant organic traffic for websites
  • Saves companies money, resources, and time

jgivoni

Date:: Aug 07, 2007

Time:: 13:53

Good question. I thought it was simple but after testing and searching it seems it is not possible to do this in a simple way.

First I thought - why not just fire the onclick instead of click?
But onclick is not a function either until it has been written explicitly in the html.
So what about setting onclick="return true"?
This would normally activate the hyperlink (whereas onclick="return false" would disable it...)
But it still doesn't do anything in Firefox.

Running out of ideas, the 'best' thing could be to add this to the A element:

<a href="..." onclick="location.href=this.href">

and then firing the onclick event instead of click().

Problems with this method:
1. It wont work If the href starts with "javascript:..." or "mailto:..." etc.
2. It wont pay respect to any TARGET attribute

These things could of course be tackled by a clever script, which I am sure someone has already written, but actually I would prefer many other things before such a script - for instance to add a submit button or similar, which would work as expected when invoking the click event.

admin

Date:: Aug 22, 2007

Time:: 13:25

dustPuppy, did you get your question solved?

If you did then please close this question and distribute the points. If you found the solution on your own, we would be very happy if you could explain it here for the sake other users having the same problem.

If you didn't get a solution to your problem, please leave a comment here to let the experts know that you're still looking for an answer.

Thanks,
The Quomon Admin Team

dustPuppy

Date:: Aug 27, 2007

Time:: 05:38

thanks, jgivoni for your response and sorry for not getting back quicker.
your proposed solution sounds like a good way to do it.
thanks for the help

Question Answered

This question has been closed, and points have been rewarded to the following experts:

jgivoni: 125

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

No tutorials have been submitted yet. Want to be the first?

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top HTML Experts

View More

Rank

Expert

Points

1.

rcastagna

1022

2.

jgivoni

928

3.

nidhi

875

4.

xarcus

240

5.

john2

220

6.

danmacryan

125

7.

Anpanman

75

8.

multani.sarbjit

75

9.

PeterNZ

75

10.

roger.berbel

75

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es