Jump to content

Onclick Causes Page To Jump To Top


Web Weenie
 Share

Recommended Posts

I have a js the uses an onclick to swap an image. It causes the page to jump back to the top of the page. I've tried to add a return false at the end of the script but this doesn't fix the problem. How can I prevent this effect from happening. Am I placing the return false incorrectly? or is there something else i need to do?TIA

Edited by Web Weenie
Link to comment
Share on other sites

What element has the click handler assigned to it? I'm thinking something like <a href="#" onclick="somefunc()"> ? If so, it's not the click. It's the href. "#"+value is used to jump the user to an internal anchor. Just "#" jumps the user to the top.Where you put the return value matters. Did you try: <a href="#" onclick="somefunc(); return false;"> ?Anyway, using links just to trigger javascript functions is bad practice. It's okay if they are also real links. If not, put your onclick attribute in a span or paragraph.Apologies if I've guessed incorrectly. By all means post some code if this doesn't solve the problem.

Edited by Deirdre's Dad
Link to comment
Share on other sites

What element has the click handler assigned to it? I'm thinking something like <a href="#" onclick="somefunc()"> ? If so, it's not the click. It's the href. "#"+value is used to jump the user to an internal anchor. Just "#" jumps the user to the top.Where you put the return value matters. Did you try: <a href="#" onclick="somefunc(); return false;"> ?Anyway, using links just to trigger javascript functions is bad practice. It's okay if they are also real links. If not, put your onclick attribute in a span or paragraph.Apologies if I've guessed incorrectly. By all means post some code if this doesn't solve the problem.
Ah-ha! I didn't think of doing it inline... that worked like a charm! Thanks . And to answer, there was no <a href>Thanks
Link to comment
Share on other sites

Yeah, if you're going to run inline javascript, the return statement has to be part of the inline code.If the element wasn't a link though, I can't imagine what made it jump. Satisfy my curiosity?
Well if there is a quirk to find, I can find it...The html that calls the js:<img src="images/scroll3.jpg" width="59" height="15" vspace="2" border="0" align="absmiddle" id="mybtn" onclick="changeimage(); return false;" />
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...