tags:

views:

22

answers:

1

Hi friends

when i click the anchor tag that hide the div, i get bump to top of page although the div gets hidden. How do i make browser stay at same place when div gets hidden by clicking that link ? here is the code

< a id="Student" href="#">Click here to hide</a> 

$('#Student').click(function(){
    $('#divHide').hide('slow');
});
+1  A: 

You need to prevent the default link behavior like this:

$('#Student').click(function(e){
  $('#divHide').hide('slow');
  e.preventDefault();
});

Or this:

$('#Student').click(function(){
  $('#divHide').hide('slow');
  return false;
});

event.preventDefault() or return false; will both prevent what the browser does by default which is going to the hash (#) for a location, causing a scroll to the top. The difference between the two is that return false also kills the event, preventing it from bubbling as well.

Nick Craver
in addition, `return false` will also trigger `stopPropagation()`
jAndy
@jAndy - It doesn't *trigger* it exactly, but yes it has the same effect (actually *more* of an effect).
Nick Craver
@Nick: well, jQuery calls `preventDefault` + `stopPropagation` on the event object if `false` is returned from the handler.
jAndy