gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
write a javascript function that scrolls to the top of the page when a button is clicked
- User
write a javascript function that scrolls to the top of the page when a button is clicked
- Assistant
You can create a JavaScript function that scrolls to the top of the page when a button is clicked like this:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Scroll to Top Example</title> </head> <body style="height: 200vh;"> <button id="scrollToTopBtn" style="position: fixed; bottom: 20px; right: 20px;">Scroll to Top</button> <script> // Get the button element const scrollToTopBtn = document.getElementById("scrollToTopBtn"); // Function to scroll to the top of the page function scrollToTop() { window.scrollTo({ top: 0, behavior: "smooth" }); } // Attach the function to the button click event scrollToTopBtn.addEventListener("click", scrollToTop); </script> </body> </html>In this example, we create a button with the ID
scrollToTopBtnand a JavaScript function calledscrollToTop(). This function useswindow.scrollToto scroll to the top of the page with a smooth behavior. Then, we attach this function to theclickevent of the button usingaddEventListener.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.