Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  193] [ 2]  / answers: 1 / hits: 11992  / 2 Years ago, tue, december 28, 2021, 12:00:00

In the same page I have multiple sections.


One section will be shown and this will happen using an active class.


In the same page I have li buttons 1, 2 and 3 and when I click on one of them the section related to it appears and the old one disappears. For that purpose I'm using Javascript.


Also in the same page I have a next and previous button. When I click on the next button the next section should appear and the old one should disappear.


And also the related li to the section should have the active class, the same thing for the previous: when I click on it it should go to the old section and the current should disappear and the li class should be active.


When I'm in the first section the previous button should disappear and when I'm in the last section the next button should disappear.


How can I implement this behavior for the next and previous buttons in this way using Javascript?


Any Help Please!!?




let tab = document.querySelector(.nav li);
let tabs = document.querySelectorAll(.nav li);
let tabsArray = Array.from(tabs);
let section = document.querySelectorAll(.section);
let sectionArray = Array.from(section);
let nextButton = document.querySelector(.next);
let prevButton = document.querySelector(.previous);
let current = 0;

tabsArray.forEach((ele) => {
ele.addEventListener(click, function (e) {
tabsArray.forEach((ele) => {
ele.classList.remove(active);
});
e.currentTarget.classList.add(active);

sectionArray.forEach((sec) => {
sec.classList.remove(active);
});
if(e.currentTarget.dataset.cont =='r1'){
prevButton.classList.add(disable);
}else{
prevButton.classList.remove(disable);
}
if (
document.querySelector(# + e.currentTarget.dataset.cont) ==
sectionArray[sectionArray.length - 1]
) {
nextButton.classList.add(disable);
} else {
nextButton.classList.remove(disable);
}
document.querySelector('#' + e.currentTarget.dataset.cont).classList.add(active);
});
});

.section {
display: none;
}

.section.active{
display: block;
}

ul {
list-style: none;
margin:0;
padding: 0;
display: flex;
align-items: center;
}

ul li {
background: #ccc;
padding: 10px 15px;
margin-left: 6px;
border-radius: 50%;
cursor: pointer;
opacity: .5;
}

ul li.active{
opacity: 1 !important;
}

.next,
.previous {
padding: 15px 10px;
border-radius: 6px;
background: deepskyblue;
color: white;
border:0;
outline: none;
cursor: pointer;
width: 100px;
}

.next.disable,
.previous.disable{
cursor: none;
opacity: .5;
}

<ul class=nav>
<li class=active data-cont=r1>1</li>
<li data-cont=r2>2</li>
<li data-cont=r3>3</li>
</ul>


<section id=r1 class=section section-one active>
<h2>section 1</h2>
</section>
<section id=r2 class=section section-two>
<h2>section 2</h2>
</section>
<section id=r3 class=section section-three>
<h2>section 3</h2>
</section>


<button class=previous disable id=previous>Previous</button>
<button class=next id=next>Next</button>




More From » html

 Answers
7

It's advisable to have a collection of buttons and another for sections, connecting the two via the index, which needs to also be tracked. If we have all this, then going next is increasing the index, while going previous is decreasing the index. I'm not making previous and next disappear, even though the question asks me to do so. Instead I'm using the disable class. If we want to make them appear/disappear, then we can use the invisible class instead.




let currentSection = 0;
let sections = document.querySelectorAll(.section);
let sectionButtons = document.querySelectorAll(.nav > li);
let nextButton = document.querySelector(.next);
let previousButton = document.querySelector(.previous);
for (let i = 0; i < sectionButtons.length; i++) {
sectionButtons[i].addEventListener(click, function() {
sections[currentSection].classList.remove(active);
sectionButtons[currentSection].classList.remove(active);
sections[currentSection = i].classList.add(active);
sectionButtons[currentSection].classList.add(active);
if (i === 0) {
if (previousButton.className.split( ).indexOf(disable) < 0) {
previousButton.classList.add(disable);
}
} else {
if (previousButton.className.split( ).indexOf(disable) >= 0) {
previousButton.classList.remove(disable);
}
}
if (i === sectionButtons.length - 1) {
if (nextButton.className.split( ).indexOf(disable) < 0) {
nextButton.classList.add(disable);
}
} else {
if (nextButton.className.split( ).indexOf(disable) >= 0) {
nextButton.classList.remove(disable);
}
}
});
}

nextButton.addEventListener(click, function() {
if (currentSection < sectionButtons.length - 1) {
sectionButtons[currentSection + 1].click();
}
});

previousButton.addEventListener(click, function() {
if (currentSection > 0) {
sectionButtons[currentSection - 1].click();
}
});

.section {
display: none;
}

.section.active{
display: block;
}

.invisible {
display: none;
}

ul {
list-style: none;
margin:0;
padding: 0;
display: flex;
align-items: center;
}

ul li {
background: #ccc;
padding: 10px 15px;
margin-left: 6px;
border-radius: 50%;
cursor: pointer;
opacity: .5;
}

ul li.active{
opacity: 1 !important;
}

.next,
.previous {
padding: 15px 10px;
border-radius: 6px;
background: deepskyblue;
color: white;
border:0;
outline: none;
cursor: pointer;
width: 100px;
}

.next.disable,
.previous.disable{
cursor: none;
opacity: .5;
}

<ul class=nav>
<li class=active data-cont=r1>1</li>
<li data-cont=r2>2</li>
<li data-cont=r3>3</li>
</ul>


<section id=r1 class=section section-one active>
<h2>section 1</h2>
</section>
<section id=r2 class=section section-two>
<h2>section 2</h2>
</section>
<section id=r3 class=section section-three>
<h2>section 3</h2>
</section>


<button class=previous disable id=previous>Previous</button>
<button class=next id=next>Next</button>




[#553] Thursday, December 16, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deonkalvinw

Total Points: 409
Total Questions: 96
Total Answers: 89

Location: Saint Pierre and Miquelon
Member since Sun, Nov 27, 2022
2 Years ago
deonkalvinw questions
Sun, Feb 6, 22, 00:00, 2 Years ago
Tue, Dec 28, 21, 00:00, 2 Years ago
Sun, Aug 22, 21, 00:00, 3 Years ago
Sun, Mar 7, 21, 00:00, 3 Years ago
;