1

Possible Duplicate:
Avoid window jump to top when clicking #-links

On my site I have links that I click and jquery handles an event for them. The links are simple, eg:

<a href=# class=bleh>click</a>

The problem is, I click a link such as that, it scrolls to the top of the page (probably because of the #).

Is there any way to make the page not scroll up at all and just stay put?

I understand I dont need the href tag, but I want it to appear as a link.

Community
  • 1
  • 1
user1022585
  • 13,061
  • 21
  • 55
  • 75

5 Answers5

1

You can try disabling the click default behavior on links with href="#".

$('[href="#"]').click(function (e) {
    e.preventDefault();
});
Richard Neil Ilagan
  • 14,627
  • 5
  • 48
  • 66
1

you can use event.preventDefault() which prevents the default action of the click event:

If this method is called, the default action of the event will not be triggered.

$('.bleh').click(function(eve){
   eve.preventDefault()
})
Ram
  • 143,282
  • 16
  • 168
  • 197
0
$('.bleh').on('click', function(e) {
   e.preventDefault();
   return false;
})
odupont
  • 1,946
  • 13
  • 16
0

Or you can use this, slightly longer but works

<a href="javascript:void(0);" class="bleh">click</a>
Ties
  • 5,726
  • 3
  • 28
  • 37
0

Placing 'void' after the '#' should stop the scrolling and still allow the event to fire.

<a href=#void class=bleh>click</a>
Web Develop Wolf
  • 5,996
  • 12
  • 52
  • 101