tags:

views:

188

answers:

9

Warning: This may be a very stupid question...

I'm currently using <A> tags with jquery to initiate things like click events, etc.

Example is <a href="#" class="someclass">Text</a>

But I hate how the '#' makes the page jump to the top of the page...what can I do instead?

+12  A: 

In jQuery, when you handle the click event, return false to stop the link from responding the usual way prevent the default action, which is to visit the href attribute, from taking place (per PoweRoy's comment and Erik's answer):

$('a.someclass').click(function(e)
{
    // Special stuff to do when this link is clicked...

    // Cancel the default action
    e.preventDefault();
});
BoltClock
ah, duh, thanks!
st4ck0v3rfl0w
@pranay: OP specified that he's using jQuery to work with these links.
BoltClock
Instead of return false, do an event.preventDefault(). This is more clear for the people who will read your code.
PoweRoy
@PoweRoy: got it. I made the edit and learned something new :)
BoltClock
@BoltClock The OP should use buttons to handle the click events instead of anchors. Your answer is pretty useful, but in the case when href points somewhere and you don't need the redirect *at the moment*. Preventing default behavior of action not needed at all is a kind of advice like: take the knife, grab the blade and hammer the nails using the handle :) Take a right tool for the job!
takeshin
@takeshin: you're right. Gave your answer a +1 :)
BoltClock
+4  A: 

You can use event.preventDefault() to avoid this. Read more here: http://api.jquery.com/event.preventDefault/.

Erik Töyrä
+1  A: 

you can even write it just like this:

<a href="javascript:void(0);"></a>

im not sure its a better way but it is a way :)

guy schaller
This method was fine for pre-HTML 4 but today it is very bad practice as it breaks too many navigation actions such as "open link in new tab".
Steve-o
you are maybe right but..in this case it dosnt matter because he is giving an onclick event so open link in new tab wouldnt work anyway...
guy schaller
+4  A: 

If you want to use a anchor you can use http://api.jquery.com/event.preventDefault/ like the other answers suggested.

You can also use any other element like a span and attach the click event to that.

$("span.clickable").click(function(){
alert('Yeah I was clicked');
});
Neothor
+1  A: 

Just use <input type="button" /> instead of <a> and use CSS to style it to look like a link if you wish.

Buttons are made specifically for clicking, and they don't need any href attributes.

The best way is to use onload action to create the button and append it where you need via javascript, so with javascript disabled, they will not show at all and do not confuse the user.

When you use href="#" you get tons of different links pointing to the same location, which won't work when the agent does not support JavaScript.

takeshin
Buttons don't work without javascript either.
kingjeffrey
Partially true ;) Form buttons work without javascript.
takeshin
A: 
<a href="javascript:"></a>
SiN
+1  A: 

If the element doesn't have a meaningful href value, then it isn't really a link, so why not use some other element instead?

As suggested by Neothor, a span is just as appropriate and, if styled correctly, will be visibly obvious as an item that can be clicked on. You could even attach an hover event, to make the elemnt 'light up' as the user's mouse moves over it.

However, having said this, you may want to rethink the design of your site so that it functions without javascript, but is enhanced by javascript when it is available.

belugabob
A: 

Just use

<a href="javascript:;" class="someclass">Text</a>

JQUERY

$('.someclass').click(function(e) { alert("action here"); }
Ayush
A: 

You could just pass an anchor tag without an href property, and use jQuery to do the required action:

<a class="foo">bar</a>

kingjeffrey
I generally don't recommend this approach, as there is no fallback action if the user has js disabled. But given you already have the href set to `#`, I assumed this is not a concern for you, and thus offered this solution as it is likely the simplest means to meet your need.
kingjeffrey
`a:link` CSS selectors will not target this anchor tag.
BoltClock
BoltClock, this is true. But st4ck0v3rfl0w did not indicate such a need.
kingjeffrey