How to prevent a click on a '#' link from jumping to top of page?
JavascriptJqueryHtmlJavascript 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>
javascript
)
Solution #2: (needed <a href="javascript:void(0);" class="someclass">Text</a>
jQuery
)
Solution #3: (needed <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();