I am experimenting with creating a simple 3 slide slider, but I am facing some issues with the JavaScript. My goal is to have the current slider slide out and the selected one slide in when clicking on the slider color. I'm attempting to achieve this by adding an active class to the clicked slider number for display. However, I can't seem to pinpoint where I went wrong. I prefer not to use jQuery as I want to practice vanilla JavaScript.
Thank you in advance!
window.onload = onPageLoad();
function onPageLoad() {
document.querySelector('.red').classList.add('active');
};
document.querySelector('.box').addEventListener('click', function() {
document.querySelector('.red').classList.toggle('active');
document.querySelector('.green').classList.toggle('active');
document.querySelector('.yellow').classList.toggle('active');
});
* {
padding: 0;
margin: 0;
}
.main__wrapper {
position: relative;
width: 100%;
height: 100vh;
overflow: hidden;
}
/* CSS styles continue... */
<div class="main__wrapper">
<section class="red"></section>
<section class="green"></section>
<section class="yellow"></section>
<div class="slide__select">
<div class="box"><span>red</span></div>
<div class="box"><span>green</span></div>
<div class="box"><span>yellow</span></div>
</div>
</div>