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 6664261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:35:27+00:00 2026-05-26T02:35:27+00:00

I am trying to add a random class to even < li > element

  • 0

I am trying to add a random class to even < li > element within my #container div

Here is my code:

$( "#container li" ).each(  

  $().ready(function() {

        $("#container li:random").addClass("one");
        $("#container li:random").addClass("two");
        $("#container li:random").addClass("three");

    })
    );

The problem is that I have ten < li > elements and I want each of these elements to receive one of the three possible classes in a random fashion.

However my code above only assignes three < li > elements with the classes leaving the remaining seven < li > elements class-less.

I have tried other snippets of code but none of which randomly apply a list of classes to ALL elements specified.

I have tried snippets from this post HERE but none of which produce the desired result of making sure all elements get a random class.

Thank you for your help.

  • 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-05-26T02:35:28+00:00Added an answer on May 26, 2026 at 2:35 am

    You can use an array of classes and then assign a random class from that array to each element matching a selector like this:

    $(document).ready(function(){
        var classes = ["one", "two", "three"];
    
        $("#container li").each(function(){
            $(this).addClass(classes[~~(Math.random()*classes.length)]);
        });
    });
    

    If you want to ensure that each class is only used once, you can remove it from the array when it is used:

    $(document).ready(function(){
        var classes = ["one", "two", "three"];
    
        $("#container li").each(function(){
            $(this).addClass( classes.splice( ~~(Math.random()*classes.length), 1 )[0] );
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my problem. I am trying to add instrumentation to my code which
Im trying to add an element to a database and then return a new
I'm trying to add some songs to a vector inside a class. One of
I'm starting with the C# code example here . I'm trying to adapt it
im trying to make an image gallery. the container class that adds the thumbnail
Trying to add objects to a ArrayDeque at random intervals. This is what I
I m trying to make a random number generator in android. My code has
Hi i'm trying to add all the space requirements of each subclass of animal
I'm trying to add a link to each slideshow image in joomla, but I've
I'm trying add a tab to my web page that looks like this: Using

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.