Scroll to Element Button
March 17, 2025 ยท AI Conversations
User
Submit
onclick nextPrev button scroll to certail id above
onclick nextPrev button scroll to certail id above
Assistant
You can modify your `nextPrev` function to scroll to a specific element above when the button is clicked. Use the `scrollIntoView` method in JavaScript to achieve this smoothly.
### Example:
<!-- Target element -->
<div id="targetSection" style="margin-top: 500px;">Scroll to me!</div>
<!-- Button -->
<button type="button" id="nextBtn" onclick="nextPrev()">Submit</button>
<script>
function nextPrev() {
// Scroll to the target element
document.getElementById("targetSection").scrollIntoView({ behavior: "smooth", block: "start" });
}
</script>
### Explanation:
– `document.getElementById("targetSection")` selects the element you want to scroll to.
– `.scrollIntoView({ behavior: "smooth", block: "start" })` ensures smooth scrolling and aligns it to the top of the viewport.
Let me know if you need any modifications! ๐