Getting # link to stay in same place when clicked, not jump to top of page

hi guys-

you know how when you have a link like:
<a href=“#” >link</a>
usually to jump to an anchor but in my case i don’t want to jump anywhere, i want to stay in the same exact spot and just run a javascript function:
<a href=“#” onclick=“myfunc()”>link</a>

the problem is upon clicking the link the function runs fine… but the page jumps to the top of the page… i want it to stay in the same exact spot. is there anyway i can do this? maybe a different thing to link to?

if you’re a little confused at what i mean there is a simple example below:

<TITLE> test Document </TITLE>


<a href="#" onclick="myfunc"><strong>Link clicking bring you to the top.... i want to stay where we are when it's clicked</strong></a>

i realize this doesn’t have anything to do with php but there was no standard html area in the forums… but i know php programmers will know exactly what i mean anyway.

thanks alot for any help!

Sure if you make the link to go to the same page but to an anchor to where you want it to be it’ll scroll to that spot. The page is going to have to reload but if you use an anchor it will go to that spot.

So instead of <a href=“#”… have <a href=“this_page.htm#myanchor”…

I’m assuming you now about anchors yeah?


There are actually several ways to achieve the effect you want. The easiest might be this:

<a href="javascript:void(0)" onclick="yourFunction()">click here!</a>

I don’t really like it though, because it shows javascript:void(0) in the status bar. The method I prefer is as follows:

<a href="#" onclick="yourFunction(); return false">click here!</a>

This kind of ‘cancels’ the click, and no scrolling will happen. Actually the href could be anything, not just “#”.
Third way to do this is not to use the A element.

<span onclick="theFunction()">click here!</span>

The styles applied for A won’t affect on SPAN elements, so I wouldn’t do it this way if I was you. Nothing will be shown in the status bar, though. This could be done with JS using onmouseover/onmouseout attributes with “windows.status = ‘’”, but I really don’t recommend doing it. Besides, I believe that users really trust links that show up in the status bar more than the ones that don’t. Atleast I do.

personally I would use the anchor method as suggested by luxinterior…

Yeah, but he doesn’t want the page to “jump” anywhere. I think it’d be waste of time to write

<a name="do_not_jump"></a><a href="#do_not_jump" onclick="method()">:(</a>

instead of

<a href="#" onclick="method(); return false">:(</a>

Or even

<a href="#" onclick="return method()">:(</a>

if method() returns false (true, if you want the link to be clicked).

correct… adding an anchor makes the page jump to the anchor… imagine if the anchor is in the middle of the page when you click the link. now the page will move so the anchor is at the top. i want no movement, don’t jump anywhere. even adding the anchor right in front of the link will still have the effect of jumping so that the anchor is at the top of the page.

the one that i used is return false … thanks for all the help guys!