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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:07:09+00:00 2026-05-17T21:07:09+00:00

basically i have a page on my site with boxes that slide to reveal

  • 0

basically i have a page on my site with boxes that slide to reveal a background color. I want this background color to be random on load of page by adding a class to the element, eg .blue, .green etc…

I have created this code and as you may notice this randomly sorts the color class and applies to the element, this works for the first 6 elements however this page in particular has 12 elements i wish this to apply to, so my question is how do i apply a random selection of the variable classes to all “.portfolio ul li a “? Would it need to have some form of a repeat?

This is my script ..

function randCol() {
return (Math.round(Math.random())*7); }

$(document).ready(function() {
var classes = [ 'blue', 'orange', 'green', 'pink', 'black', 'white'];
classes.sort( randCol );
$('.portfolio ul li a').each(function(i, val) {
    $(this).addClass(classes[i]);
});

})

This is my source code structure

<div class="portfolio">
<ul>
    <li>
       <a href="#">
         <img src="images/content/portfolio/sample1.png" height="175" width="294" alt="sample" class="front" />
         Text for behind the image                         
       </a>
    </li>

  • 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-17T21:07:10+00:00Added an answer on May 17, 2026 at 9:07 pm

    Your randCol() comparison function doesn’t really work. In the first place, such a function should return 1, -1 or 0 depending on whether the first term of the comparison is greater than, less than, or equal to (respectively) the second term.

    Second, the code you have in there is apparently intended to reference your 6-element array, but as written it can return values as high as 7, which is 2 higher than the last index of the array. Better to do your randoming within the each() function literal, like so:

    $(document).ready(function() {
    var classes = [ 'blue', 'orange', 'green', 'pink', 'black', 'white'];
    
    $('.portfolio ul li a').each(function(i, val) {
        var index = Math.floor(Math.random()*6); // highest value will be 5, lowest will be 0
        $(this).addClass(classes[index]);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have a single page on my site that I want any php
Basically I have a single page site that scrolls when the user clicks on
The home page of this site will have basically a 3 column layout. I
Basically I have MVC site with few tabs on my page, that uses JavaScript.
In this html I basically have a sidebar-div and a page-div with the header,
I have a page where I basically want an element to 'blink' for a
I have a page that will basically be used to concatenate a bunch of
I have this page which has three peson searchs on them. Basically it's a
I'm building this website and I have basically two questions about this page: http://sites.publishyours.com.br/silviamecozzi/pt/obra/deserto_das_palmas.php
I have a JSF page that is included in other JSF pages (basically a

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.