I’m trying to create a scroll button for a website but it’s not working...Any help please?

Any help with this please??
I’m just learning JavaScript and I want to create a scroll button that takes one back to the top of a website after viewing the website till the end.
There’s probably a lot wrong with the code but I don’t even know.
Thanks a lot.

Keeps giving me this error code

Without seeing the actual code (like, in a https://codepen.io/ or a https://repl.it/ ), debugging is simply poking a stick in the dark.


okay, will try doing that now

How about now??

Now that you’ve put it in codepen, are you seeing a red warning in the JS pane? There are errors.

First, you define the var lines in the first two, but that var keyword on the second is causing an error.

Then, later on, you have an if statement. Looks like

      if (docHeight != "undefined") (
      	offset =  docHeight / 4;

Here’s the issue - if statements are followed by curly braces ({...}), not parentheses ((...)).

Same in the function definition you’re writing for your callback function: use curly braces.

I’m still not getting it sorry. Can you please check it again?

What exactly did you change based on @snowmonkey’s suggestion?

I can not see that you changed anything.

You have var on the second line, but your previous line ends in a comma. You can change the comma to a semi-colon (on that line and the others) or you can just use the word var on the very first line and remove the other var references (that are currently separated by commas).

Also, he already told you that if statement code blocks use curly brackets/braces and not parentheses. You still need to fix that issue too.

You have a similar probblem with the line that starts as:

window.addEventListener("scroll", function (event)

Lastly, the lines involving the first line involving the addEvenListener should be moved to the very bottom of your code and not separated by the commas.

1 Like

Thank you so much @snowmonkey and @RandellDawson, the error message has stopped now but my web console is still saying “contactBtn is null” and the code for the scroll button doesn’t seem to work. Thanks for your inputs so far.

When I look at the latest version of your pen, you just need to delete the invalid syntax at the end of the JavaScript section. You have a /* that should not be there. Removing that makes everything work from what I can tell.

One of the huge advantages of using codePen, it’s really great at putting error messages right out there for you. Click on that red icon in the code editor, it’ll pop an error message right near the error itself. And fixing the error? Removes the message. And usually moves on to the NEXT error.

Bug squashing, serving developers since 1967. :wink:

It’s working fine now, thanks so much @RandellDawson and @snowmonkey