I want you to help me create a javascript or jquery to scroll up when a button is click…
- User
I want you to help me create a javascript or jquery to scroll up when a button is click :
Button in question :
Scholl up to :
- Assistant
To create a JavaScript or jQuery code that scrolls up to a specific section when a button is clicked, you can follow the steps below. I'll provide you with two solutions: one using pure JavaScript and the other using jQuery, assuming that jQuery is already included in your project.
Pure JavaScript Solution
document.addEventListener("DOMContentLoaded", function() { // Listen for a click event on the button document.querySelector("#payment-confirmation .btn.btn-primary").addEventListener("click", function() { // Scroll to the checkout-payment-step section document.getElementById("checkout-payment-step").scrollIntoView({ behavior: 'smooth' }); }); });This code waits until the DOM is fully loaded, then adds a click event listener to the button inside the
#payment-confirmationdiv. When the button is clicked, it uses thescrollIntoViewmethod to smoothly scroll to the<section id="checkout-payment-step">.jQuery Solution
If you prefer using jQuery, which might simplify the syntax a bit, the solution could look like this:
$(document).ready(function() { // Listen for a click event on the button $("#payment-confirmation .btn.btn-primary").click(function() { // Scroll to the checkout-payment-step section $('html, body').animate({ scrollTop: $("#checkout-payment-step").offset().top }, 'slow'); }); });This jQuery script does essentially the same thing: waits until the DOM is fully loaded, then adds a click event listener to the button within the
#payment-confirmationdiv. On click, it animates the scroll to bring the<section id="checkout-payment-step">into view. ThescrollTopproperty is set to the top offset of the target section, and the'slow'parameter makes the scroll smoothly animate.Both of these solutions will achieve the scroll-up effect you're looking for when the "COMMANDER" button is clicked. Choose the one that best fits the rest of your project setup or your personal coding preferences.