thinker
thinker

Reputation: 412

Unnecessary scrolling to the top of the page when clicking # link

I have a link on page <a href="#"... which linked with jquery function. After link was clicked the page scrolled to the top and this seems unnecessary behavior. Is there a way to prevent scrolling?

Upvotes: 0

Views: 74

Answers (3)

Komeil Tl
Komeil Tl

Reputation: 24

you can use this : (prevent click)

<a href="javascript(0)" >...</a>

Upvotes: 0

Ionut Necula
Ionut Necula

Reputation: 11472

On click of your button you can prevent the default action using preventDefault():

 $('a[href="#"]').on('click', function(event) {
   event.preventDefault();
   console.log('default action prevented');
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href='#'>Button</a>

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

You can prevent following of # by selecting them and using event.preventDefault():

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

Upvotes: 1

Related Questions