How do I scroll to the top of the page using JavaScript? The scrollbar instantly jumping to the top of the page is desirable too as I\'m not looking to achieve smooth scroll
This simple solution works natively and implements a smooth scroll to any position.
It avoids using anchor links (those with #
) that, in my opinion, are useful if you want to link to a section, but are not so comfortable in some situations, specially when pointing to top which could lead to two different URLs pointing to the same location (http://www.example.org and http://www.example.org/#).
Put an id to the tag you want to scroll to, for example your first section, which answers this question, but the id could be placed everywhere in the page.
Then as a button you can use a link, just edit the onclick attribute with a code like this.
Click me
Where the argument of document.getElementById
is the id of the tag you want to scroll to after click.