How to prevent a click on a '#' link from jumping to top of page?

JavascriptJqueryHtml

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

So this is old but... just in case someone finds this in a search.

Just use "#/" instead of "#" and the page won't jump.

Solution 2 - Javascript

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();
});

Solution 3 - Javascript

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 :)

Solution 4 - Javascript

Solution #1: (plain)
<a href="#!" class="someclass">Text</a>

Solution #2: (needed javascript)
<a href="javascript:void(0);" class="someclass">Text</a>

Solution #3: (needed jQuery)
<a href="#" class="someclass">Text</a>
<script>
$('a.someclass').click(function(e) {
    e.preventDefault();
});
</script>

Solution 5 - Javascript

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

Solution 6 - Javascript

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.

Solution 7 - Javascript

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');
});

Solution 8 - Javascript

$('a[href="#"]').click(function(e) {e.preventDefault(); });

Solution 9 - Javascript

You can use #0 as href, since 0 isn't allowed as an id, the page won't jump.

<a href="#0" class="someclass">Text</a>

Solution 10 - Javascript

There are 4 similar ways to prevent the page from jumping to the top without any JavaScript:

Option 1:

<a href="#0">Link</a>

Option 2:

<a href="#!">Link</a>

Option 3:

<a href="#/">Link</a>

Option 4 (Not recommended):

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

But it's better to use event.preventDefault() if you are handing the click event in jQuery.

Solution 11 - Javascript

Solution 12 - Javascript

Just use

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

JQUERY

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

Solution 13 - Javascript

Links with href="#" should almost always be replaced with a button element:

<button class="someclass">Text</button>

Using links with href="#" is also an accessibility concern as these links will be visible to screen readers, which will read out "Link - Text" but if the user clicks it won't go anywhere.

Solution 14 - Javascript

The #/ trick works, but adds a history event to the browser. So, clicking back doesn't work as the user may want/expect it to.

$('body').on('click', 'a[href="#"]', function(e) {e.preventDefault() }); is the way I went, as it works for already existing content, and any elements added to the DOM after load.

Specifically, I needed to do this in a bootstrap dropdown-menu inside of a .btn-group[(Reference)][1], so I did:

$('body').on('click', '.dropdown-menu li a[href="#"]', function(e) {e.preventDefault() });

This way it was targeted, and didn't affect anything thing else on the page.

[1]: http://getbootstrap.com/components/#btn-dropdowns "reference"

Solution 15 - Javascript

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

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

Solution 16 - Javascript

I have used:

<a href="javascript://nop/" class="someClass">Text</a>

Solution 17 - Javascript

I've always used:

<a href="#?">Some text</a>

when trying to prevent the page jump. Not sure if this is the best, but it seems to have been working well for years.

Solution 18 - Javascript

You can also return false after processing your jquery.

Eg.

$(".clickableAnchor").live(
    "click",
    function(){
        //your code
        return false; //<- prevents redirect to href address
    }
);

Solution 19 - Javascript

I use something like this:

<a href="#null" class="someclass">Text</a>

Solution 20 - Javascript

To prevent the page from jumping, you need to call e.stopPropagation(); after calling e.preventDefault();:

stopPropagation prevents the event from going up the DOM tree. More info here: https://api.jquery.com/event.stoppropagation/

Solution 21 - Javascript

If you want to migrate to an Anchor Section on the same page without page jumping up use:

Just use "#/" instead of "#" e.g

<a href="#/home">Home</a>
<a href="#/about">About</a>
<a href="#/contact">contact</a> page will not jump up on click..

Solution 22 - Javascript

Adding something after # sets the focus of page to the element with that ID. Simplest solution is to use #/ even if you are using jQuery. However if you are handling the event in jQuery, event.preventDefault() is the way to go.

Solution 23 - Javascript

The simplest one for me was to do this.

<a href="#" onclick="return false;">Some text</a>

The reason for using JS is that most modern sites rely on it.

Solution 24 - Javascript

The <a href="#!">Link</a> and <a href="#/">Link</a> does not work if one has to click on the same input more than once.. it only takes in 1 event click for each on multiple inputs.

still the best way to go is with <a href="#">Link</a>

then,

event.preventDefault();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionst4ck0v3rfl0wView Question on Stackoverflow
Solution 1 - JavascriptChrisView Answer on Stackoverflow
Solution 2 - JavascriptBoltClockView Answer on Stackoverflow
Solution 3 - Javascriptguy schallerView Answer on Stackoverflow
Solution 4 - JavascriptNabi K.A.Z.View Answer on Stackoverflow
Solution 5 - JavascriptErik Töyrä SilfverswärdView Answer on Stackoverflow
Solution 6 - JavascripttakeshinView Answer on Stackoverflow
Solution 7 - JavascriptNeothorView Answer on Stackoverflow
Solution 8 - JavascriptPraveen ManneView Answer on Stackoverflow
Solution 9 - JavascriptVeveView Answer on Stackoverflow
Solution 10 - JavascriptExilView Answer on Stackoverflow
Solution 11 - JavascriptbelugabobView Answer on Stackoverflow
Solution 12 - JavascriptAyushView Answer on Stackoverflow
Solution 13 - JavascriptmartinedwardsView Answer on Stackoverflow
Solution 14 - JavascriptDavidView Answer on Stackoverflow
Solution 15 - JavascriptkingjeffreyView Answer on Stackoverflow
Solution 16 - JavascriptBrice RoncaceView Answer on Stackoverflow
Solution 17 - JavascriptJosh HarrisView Answer on Stackoverflow
Solution 18 - Javascriptuser3581326View Answer on Stackoverflow
Solution 19 - JavascriptstefanView Answer on Stackoverflow
Solution 20 - JavascriptanthonyjruffaView Answer on Stackoverflow
Solution 21 - JavascriptAgidigbi Ayodeji VictorView Answer on Stackoverflow
Solution 22 - JavascriptVivek VijayanView Answer on Stackoverflow
Solution 23 - JavascriptFiddlingAwayView Answer on Stackoverflow
Solution 24 - JavascriptRichard RosarioView Answer on Stackoverflow