Make button scroll to top of page

I have a button in the image with text element, and I’d like it to scroll the user to the top of the page when clicked. I used this code before, but it only worked in the customize section, not on the actual website.

html { scroll-behavior: smooth; } document.addEventListener('DOMContentLoaded', function() { let buttonScrollTop = document.querySelector('.button[data-shopify-editor-block*="button_PfN8Qi"]'); if(buttonScrollTop) { buttonScrollTop.addEventListener('click', function(e){ e.preventDefault(); document.body.scrollTop = 0; document.documentElement.scrollTop = 0; }); } });

Hey @Viajey ,

Welcome to the community!

Can you send over the link and the store password?

Hi @Viajey

Maybe you get an element of the editor, so it works only for the editor. Please check on live site and get correct element of button to make it work

For sure. The website is viajey.com and the password is abc123

This is what the element is on the live website. How would I add this to my code?

Buy Now

Could you please show me the code and indicate which section you would like to scroll to when the button is clicked?

The elements are on this link, and the store password is abc123

https://viajey.com/products/wrap-travel-pillow

The “Buy Now” button in the first image should be clicked to bring the user to the section in the second image.

Simply add this value in the link of that button

#MainProduct-template--16290336473132__main
1 Like

it worked! Thank you!