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

  • SEARCH
  • Home
  • 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 8334655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:25:32+00:00 2026-06-09T03:25:32+00:00

I have 3 (at the moment, could be more in the future) styles that

  • 0

I have 3 (at the moment, could be more in the future) styles that I want to add to a group of divs in rotating order.

The classes I want to assign are:
.blue, .red, .green

and I have 4 divs that I want to assign them to in rotating order:

<div class="tripItem">
   ...
</div>
<div class="tripItem">
   ...
</div>
<div class="tripItem">
   ...
</div>
<div class="tripItem">
   ...
</div>

I would like the end result to be:

<div class="tripItem blue">
   ...
</div>
<div class="tripItem red">
   ...
</div>
<div class="tripItem green">
   ...
</div>
<div class="tripItem blue">
   ...
</div>

and to keep on rotating through in the same order if I was to add more divs.

What’s the best way to go about this?

  • 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-09T03:25:34+00:00Added an answer on June 9, 2026 at 3:25 am

    You can pass a callback to addClass [docs]:

    var classes =  ['blue', 'red', 'green'],
        len = classes.length;
    
    $('div.tripItem').addClass(function(i) {
        return classes[i%len];
    });
    

    The modulus operator is your friend if you want to rotate through a list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At the moment I have a DocumentViewer in a WPF window that displays an
At the moment I have a combobox that is populated from the name fields
I'm designing my personal website at the moment and want to have images inside
At the moment, I have a sprite that I have arbitrarily set to move
At the moment I have a custom ActiveX plugin that drops down the usual
I have a script that parses IIS logs and at the moment it fetches
I'm a bit confused at the moment regarding how to have a more maintainable
I have a Silverlight application where I periodically load more data and add it
At the moment I have a program that demonstrates hypotrochoids and epitrochoids (aka Spirograph
I have business logic that could either sit in a business logic/service layer or

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.