Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8261669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:29:20+00:00 2026-06-08T03:29:20+00:00

I am trying to add and remove a class within a <span> , but

  • 0

I am trying to add and remove a class within a <span>, but the removeClass is not working though the function is being called.

setInterval(changeClass,4000);

var spanId=1;

function changeClass(){
 $('#'+spanId).addClass("hilite");
 setTimeout(remove, 1000);
 spanId++;}  

function remove(){
 $('#'+spanId).removeClass("hilite");
  return true;
  }

Can any one tell the reason ?

How can I do this with a while loop something like this i tried all my ways but chould not get it working

var spanSet=4;
var spanId=1;
while(spanSet > 0)
{
changeClass();
spanSet--;
}

function changeClass(){
$('#'+spanId).addClass("hilite");
setTimeout(remove, 3000);
}

function remove(){
$('#'+spanId).removeClass("hilite");
spanId++;
return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:29:22+00:00Added an answer on June 8, 2026 at 3:29 am
    function changeClass(){
     $('#'+spanId).addClass("hilite");
     setTimeout(remove, 1000);
     spanId++;} 
    

    In this code, you should execute spanId++ in a callback function which is passed as parameter to setTimeout since JS won’t halt the execution until it finishes setTimeout statement.

    So your code should look like this:

    function changeClass(){
     $('#'+spanId).addClass("hilite");
     setTimeout(remove, 1000);
    }  
    
    function remove(){
      $('#'+spanId).removeClass("hilite");
      spanId++;
      return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to remove a class attribute from an element and add the class
Im new to asp.net mvc. I'm trying add new model class but it got
I'm trying to manipulate two elements within a jquery function and i'm not seeing
Trying to add an incremented class member variable 'nNodeIndex' to arraylist, but at the
I'm trying to add a class to the selected radio input and then remove
I'm having a couple problems trying to add/remove a class from links and their
I'm trying to write a function in jQuery that will add a class to
I am trying to add a highlight color to a class like this $(".common_box").hover(function(){
I am using a jQuery function to add/remove a class to the clicked element,
I am trying to add/remove UILabels to a ScrollView. The adding takes place just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.