0

I am new to javascript and I have been playing around with prevent default command for ajax pagination and I have the following code:

http://jsfiddle.net/6pqfH/2/

$('.pagination').click(function(e){
    e.preventDefault();
    // fade out current content
    $('.results').css("opacity", "0.5");
    // load new content
    // ....
    // unfade
    $('.results').css("opacity", "1");
    // go back up to top of the page
    $('html,body').scrollTop(0);
});

However, it doesn't seem to be working, it links to a new page instead of executing the fade in and out and bringing it back to the top of the page. I have checked the code for the opacity and scoll and it seems to be right so where am I going wrong here?

4
  • 3
    From my console: SyntaxError: illegal character @ http://fiddle.jshell.net/_display/:27 (# isn't a JavaScript comment character.) Your console (Ctrl+K in FF, Ctrl+J in Chrome, F12 in IE) should be your first stop for diagnosing errors. Commented May 8, 2013 at 12:49
  • 2
    @apsillers: typical programmers mistake dude, just replace them with // and proceed with your analysis Commented May 8, 2013 at 12:52
  • Thank you for the tip, it seemed to show it as a comment in jfiddle but I have learnt something now. Commented May 8, 2013 at 13:05
  • 1
    @Jimmy:is this what you wanted ? jsfiddle.net/6pqfH/8 Commented May 8, 2013 at 13:06

2 Answers 2

2

It does both steps, The reason why you do not see it is that it does it right away. You need to add a delay or animation to break it up.

$('.pagination').click(function(e){
    e.preventDefault();
    // fade out current content
    $('.results').css("opacity", "0.5");
    // load new content
    // ....
    // unfade
    $('.results').fadeTo('slow', 1, function() {
      $('html,body').scrollTop(0);
    });

});
Sign up to request clarification or add additional context in comments.

Comments

0

here's what I think you want:

http://jsfiddle.net/6pqfH/6/

<a class="pagination" id="num" href="#">4</a>  

I replaced the hred="/test" by href="#" this way the a will take you to the top alone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.