Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda