I'm practicing a bit of JavaScript and trying to make the bases of a slider I ran into a problem.
the problem is this:
When I click on the previous or next arrows, the function is not executed at the first click as it happens when you select it directly, it simply does not do anything and it is until the second click that is executed.
I attach the functions and the code in codepen at the end.
function previousItem() {
// si el indice es igual a 0
if(index == 0) {
index = botones.length; //el indice es igual es igual a length de los botones
}
// El indice disminuye en cada click
index -= 1;
// Establece el indice activo donde el inicial es el 0
activeIndex = botones[index];
for (let i = 0; i < botones.length; i++) {
const BOTON:any = botones[i];
//si el Boton es igual el indice activo agrega estilo
if(BOTON == activeIndex) {
BOTON.style.color = "orange";
} else {
BOTON.style.color = "#4c4c4c"; // Si no lo resetea
}
}
};
function nextItem() {
// si el indice es igual la length de botones
if(index == botones.length) {
index = 0; //el indice es igual a 0
}
// Establece el indice activo donde el inicial es el 0
activeIndex = botones[index];
for (let i = 0; i < botones.length; i++) {
const BOTON:any = botones[i];
//si el Boton es igual el indice activo agrega el estilo
if(BOTON == activeIndex) {
BOTON.style.color = "orange";
} else {
BOTON.style.color = "#4c4c4c"; // Si no lo resetea
}
}
// El indice aumenta en cada click
index += 1;
};
Here I enclose everything I try to do by codepen.
See the Pen Active without classes by Flixwick ( @Flixwick ) on CodePen .Thanks to those who respond.