Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

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

 

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

aviavital

Date:: Jan 12, 2010

Time:: 18:35

great, 3 years after..
I'm using a hidden link to upload files
after selecting them from list

onlyab

Date:: Dec 28, 2010

Time:: 23:29

Thanks the author, very useful advice. my pc's internet connection is very slow, which always drive me crazy, I want a program to solve my slow computer, several days ago my friend recommend me a software tuneup360, now I'm waiting for the result.

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 Experts

View More

Rank

Expert

Points

1.

nidhi

10354

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

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