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

125
Visualizações
Loop through ol in jQuery and remove class and text

I have to loop through a ol and then get each of the li and then remove the text and add "<a" element to it. Following is the html that is rendered:

<ol class="progress">
  <li class="list-group-item text-muted list-group-item-success active">
   General Information<span>1</span>
  </li>
</ol>

I want to remove all the classes except "active" and then wrap the text with General Information Like below

<ol class="progress">
  <li class="active">
   <a href="#">General Information<span>&nbsp;1</span></a>
  </li>
</ol>

I have tried to loop through the Ol using the script below but it seems to not find anything

$('ol.progress').each(function (i, li) {
 var listItem = li;
 var lm = $(li).text();
 console.log(lm); 
});
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

With vanilla js:

document
  .querySelectorAll('ol.progress > *') // all children of <ol> with class .progress
  .foreach(elem => {
    elem.className = 'active'
    elem.innerHTML = `<a href="#">${elem.innerHTML}</a>`
  })
about 4 years ago · Juan Pablo Isaza Relatório

0

I see you are using JQuery - so let's stick to this.

You are looping over the ol but you want to loop over the ol's lis. So replace

$('ol.progress').each....

by

$('ol.progress li').each....

You can remove all classes with the .removeClass() without any parameters. But as you want to preserve the active-class you have to preserve this attribute - in my version of the code (see below) I will use a variable for this.

In addition you want to replace the HTML-part and not the text-part - so your snippet would read

$('ol.progress li').each(function () {
  var is_active = false;
  is_active = $(this).hasClass('active');
  $(this).removeClass();
  if(is_active){
    $(this).addClass('active');
  }
  var newcontent = '<a href="#">' + $(this).html() + "</a>";
  $(this).html(newcontent);
});
about 4 years ago · Juan Pablo Isaza Relatório

0

With jquery, as you can see, I check if the element has a class, then I remove all the classes and add active if it had it.

$('ol.progress li').each(function(i, li) {
  var listItem = li;
  if ($(li).hasClass('active')) {
    $(li).removeAttr('class');
    $(li).addClass('active');
  } else {
    $(li).removeAttr('class');
  }
});
.active {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ol class="progress">
  <li class="list-group-item text-muted list-group-item-success active">
    General Information<span>1</span>
  </li>
  <li class="list-group-item text-muted list-group-item-success">
    General Information<span>2</span>
  </li>
  <li class="list-group-item text-muted list-group-item-success">
    General Information<span>3</span>
  </li>
</ol>

In your code you forgot li into selector so $('ol.progress li') instead of $('ol.progress')

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