I’m trying to build the next and prev buttons to switch between sections on the same page in HTML using JavaScript
@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap");
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
font-family: "Inter", sans-serif;
color: #343a40;
}
.nav{
background-color: orange;
top:0;
lef:0;
position: fixed;
}
#section-1{
height:100vh;
background-color:green;
}
#section-2{
height:100vh;
background-color:red;
}
#section-3{
height:100vh;
background-color:blue;
}
<section id="section-1">
<!-- content of section 1 -->
</section>
<section id="section-2">
<!-- content of section 2 -->
</section>
<section id="section-3">
<!-- content of section 3 -->
</section>
<div class="nav">
<button id="prev-btn">Previous</button>
<button id="next-btn">Next</button>
<button id="reset-btn">home</button>
</div>
I tried to make a counter to change the sections when the button clicked to go to the next one but it didn’t work
3
Answers
Thanks for your answers it's inspiring me to come up with this solution
Method 1: Create separate pages for the sections, and make the buttons links.
Method 2: Make an array of backticked strings that contain HTML data for each section per string, and let the buttons change the page data as per the array. The previous button would update the page with the previous array element’s data, and the next button would update the page with the next array element’s data.
Html code
JavaScript code