Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!