views:

3534

answers:

8

I'm working on some JQuery to hide/show some content when I click a link. I can create something like:

<a href="#" onclick="jquery_stuff" />

But if I click that link while I'm scrolled down on a page, it will jump back up to the top of the page.

If I do something like:

<a href="" onclick="jquery_stuff" />

The page will reload, which rids the page of all the changes that javascript has made.

Something like this:

<a onclick="jquery_stuff" />

Will give me the desired effect, but it no longer shows up as a link. Is there some way to specify an empty anchor so I can assign a javascript handler to the onlick event, without changing anything on the page or moving the scrollbar?

+14  A: 

Put a "return false;" on the second option:

<a href="" onclick="jquery_stuff; return false;" />
Otávio Décio
Perfect, thanks :)
Alex Fort
Completely unnecessary. Just make href="javascript:code stuff". It'll never jump anywhere.
Robert C. Barth
"return false;" is the correct way to do it. A javascript: URL can still replace the page if it returns a non-null object, and gives an ugly JavaScript error on functions like open-in-new-tab.
bobince
Thanks! helped me too.
dscher
A (possible) problem with href="javascript:code stuff" is that then the user then sees the contents of the javascript in the status bar. Kinda undesirable IMHO.
humble coffee
@humble coffee i think it adds some transparency for the user, personally i'd like to know what a link does before i click it.
Sam
+1  A: 
<a href="javascript:// some helpful comment " onclick="jquery_stuff" />
Diodeus
+9  A: 

You need to return false; after the jquery_stuff:

<a href="no-javascript.html" onclick="jquery_stuff(); return false;" />

This will cancel the default action.

Greg
I like the idea of providing a target for the non-javascript users. It would be especially useful if the target page did something similar to what the javascript would be doing...
rmeador
Depending on what the JS is doing, this is considered the most accessible for not JS users. +1
cdeszaq
A: 

<a href="javascript:;" onclick="jquery">link</a>

href requires something in there if you want it to not pop up errors in validators for html. the javascript:; is a good place holder; If you really want to use the #

<a href="#me" name="me" onclick="jquery">link</a>

be careful with the return false; it halts default behaviours of whatever you are doing.

Also if your js is like a submit you may run into problems in internet explorer.

hope this helps

Dean
+2  A: 

you can put simply as like below

<a href="javascript:;" onclick="jquery_stuff">

Srikanth
+1  A: 

I usually use a <span> and style it to look like a link when I need to accomplish something like this.

Matthew
I'm quite sure this makes the web site inaccessible with a keyboard, because the onclick will not fire when you press Enter, even though you have made the span selectable by adding a tabindex to it. This is important to test, if you are developing a public web site that needs to follow the WCAG guidelines.
Jan Aagaard
A: 

I seem to have a similar problem but I have been unable to come to a solution. The test site is http://altuit.on-rev.com/cig-test - Is there a way to make the links on the left display the correct content in the center but not "jump" the page?

A: 

Usually I do:

<a style="cursor:pointer;" onclick="whatever();">Whatever</a>
Sarun