gjjr
gjjr

Reputation: 569

Having an issue getting JS script to work, can anyone see the problem here?

Im pretty sure the code Im using is correct but its not working and I cant figure out why. Its to make a button scroll back to the top of the page, so Im loading the jQuery library in the tags, and then the script before the end of the tag.

<head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

    </head>
<div>
                <h1 id="backtotop" style="font-size:250%; text-align:center;">BACK TO TOP</h1>
            
         </div> 

        <script>
        jQuery(document).ready(function($){

    // scroll body to 0px on click
    jQuery('#backtotop').click(function () {
        jQuery('body,html').animate({
            scrollTop: 0
        }, 1600);
        return false;
    });
});
  });
      </script>  

Upvotes: 0

Views: 42

Answers (2)

Bravo
Bravo

Reputation: 6254

You have an extra }); at in your code

See snippet

jQuery(document).ready(function($) {

  // scroll body to 0px on click
  jQuery('#backtotop').click(function() {
    jQuery('body,html').animate({
      scrollTop: 0
    }, 1600);
    return false;
  });
});
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

</head>
<div style="height:1000px;background:#eee"></div>
<div>
  <h1 id="backtotop" style="font-size:250%; text-align:center;">BACK TO TOP</h1>

</div>

Upvotes: 2

Mikkel Gundersen
Mikkel Gundersen

Reputation: 52

As the guy who commented says, you can use jquery using $. And you also had too many close brackets.

This here works: $(document).ready(function(e){

// scroll body to 0px on click $('#backtotop').click(function () {

$('body,html').animate({
    scrollTop: 0
}, 1600);
return false;
    });
});

Here is a JSFiddle to check: https://jsfiddle.net/sparkmig/43f6e02L/3/

Upvotes: 0

Related Questions