0

I'm trying to make a page scroll to top when a link is clicked. Heres what I got so far.

<script>
jQuery(document).ready(function () {  
    jQuery("ul.pager a").click(function() { 
       scroll(0,0);  
       return false;
  });
});
<script>

Problem with that code is that it works only on first click.

How to make it work every time I click on a link?

6
  • works for me: jsfiddle.net/794fn/3 Commented Oct 27, 2011 at 17:10
  • It looks like there's something else going on in your code that's messing things up. Here's a fiddle that shows that your idea should work. Can you post more of your code, or link to an example or something? Commented Oct 27, 2011 at 17:10
  • Your </script> tag is wrong here. I don't know if that's the actual HTML you used, though. Can we have a jsFiddle?
    – Ry-
    Commented Oct 27, 2011 at 17:11
  • Its not the script tag its just a typo here. Commented Oct 27, 2011 at 19:06
  • Problem could be that once i invoke that function the whole pager that should trigger that also gets refreshed but js code dosent. Commented Oct 27, 2011 at 19:07

2 Answers 2

1

Maybe you could use internal links (href="#top") instead of binding events with jQuery.

1
  • That solution is not possible in my case Commented Oct 27, 2011 at 19:29
0
jQuery(document).ready(function () {  
    jQuery("ul.pager a").live("click", function() { 
       scroll(0,0);  
       return false;
  });
});

try this

4
  • what if you move the script tag out of the pager that is getting refreshed to some other place? Commented Oct 27, 2011 at 19:33
  • Its not in a pager that is getting refreshed. script is there all the time and pager is refreshed which should trigger the function Commented Oct 27, 2011 at 19:58
  • can you give an example of the href attributes of links inside the page? Commented Oct 27, 2011 at 20:07
  • Tnx all for trying but I managed to solove it. Had to hack a bit Drupal views js but that seems to be the only solution for now. Now script loads every time pager loads and it works. Commented Oct 27, 2011 at 20:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.