Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
slider shows one extra condition in jquery

I have html file like this:

<ul class="slider-section__toggle-ctas">
          <li class="slider-section__toggle-cta active"><span>red</span></li>
          <li class="slider-section__toggle-cta"><span>green</span></li>
          <li class="slider-section__toggle-cta"><span>blue</span></li>
        </ul>

and js like this:

const imagesSlider = () => {
let nextBtn = "";

if ($(".slider-section__toggle-cta").text().length !== 0) {
      nextBtn = $(".active").next();
    } 

if ($(".slider-section__toggle-cta").text().length === 0){
      nextBtn = $(".slider-section__toggle-cta:first");
    }

console.log($(".active").text().length)

$('.active').removeClass('active');

nextBtn.addClass("active");

}
setInterval(imagesSlider, 3000);

there is one extra condition according to that console.log() that i included in the code, and i don't know how to fix it, so basically when the length is 3 it shows the green, when the length is 5 it shows blue and when the length is 4 it shows nothing!(that's the problem), I want to go back to the first li with "red" text here,

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The problem is that you're checking the .slider-section__toggle-cta text size which is always not empty in the following if statment.

if ($(".slider-section__toggle-cta").text().length === 0)

Use it this way:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="slider-section__toggle-ctas">
  <li class="slider-section__toggle-cta active"><span>red</span></li>
  <li class="slider-section__toggle-cta"><span>green</span></li>
  <li class="slider-section__toggle-cta"><span>blue</span></li>
</ul>


<script type="text/javascript">
    
const imagesSlider = () => {
  let nextBtn = "";
 
  if ($(".active").text().length !== 0) {
    nextBtn = $(".active").next();
  }

  if ($(".active").next().text().length === 0) {
    nextBtn = $(".slider-section__toggle-cta:first");
  }

  console.log($(".active").text().length)

 
  $('.active').removeClass('active');
  nextBtn.addClass("active");

}
setInterval(imagesSlider, 3000);

</script>

Look at this demo

about 4 years ago · Juan Pablo Isaza Denunciar

0

i fixed it using

  if ($(".active").next().text().length !== 0) {
      nextBtn = $(".active").next();
    } 

    
    if ($(".active").next().text().length === 0) {
        nextBtn = $(".slider-section__toggle-cta:first");
      }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda