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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:08:38+00:00 2026-05-25T11:08:38+00:00

First, a jsfiddle… http://jsfiddle.net/therocketforever/jYba3/11/ // Highlight selected linker link & set all others to

  • 0

First, a jsfiddle…
http://jsfiddle.net/therocketforever/jYba3/11/

//  Highlight selected linker link & set all others to default.  
    $('a.linker').click(function(){
    $(this).addClass('selected');
    $(this).parent('li').siblings().find('.selected').removeClass('selected');

//  Selects a random colour from the 'colors' array by getting a random value 
//  between 0 and the length of the color array.   
    rand = Math.floor(Math.random()*colors.length);
    $(this).css("background-color", colors[rand]);

Now a Question,

Firstly this code works almost exactly the way that I would like it to, A user clicks a link, the selected colour is applied to the link text, removed from the others & the background of the link is set to a random color from the array of colours. Cool.

What I would like to know is… How would I make it so that the randomly set background colour is removed from the non selected links (ie. Only the link with the .selected class has the background colour.)

EXTRA CREDIT

Bonas points if the same background colour is never used twice in a row. (ie. If click one sets to yellow, click two is any other colour except yellow.

  • 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-25T11:08:38+00:00Added an answer on May 25, 2026 at 11:08 am

    This’ll meet all your requirements (bonus included).

    $(document).ready(function(){
    
    //  Colours for links
    var colors = ["#fff766","#66cef5","#bd7ebc","#66cc66"];
    
    $("a.linker").click(function(){        
        var $this = $(this).addClass("selected");
    
        $this.parent('li').siblings().find('.selected')
            .removeClass('selected').css("background-color", "");
    
        var num = $this.data("colorNum"),
            rand = num;
    
        while (num === rand) {
            rand = Math.floor(Math.random()*colors.length);
        }
    
        $this.css("background-color", colors[rand])
            .data("colorNum", rand);
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, here is the fiddle link on what I am stucked in: http://jsfiddle.net/FYHMV/ I
First of all, here is a JSFiddle which has the problem I'm getting. http://jsfiddle.net/UG66K/9/
See this fiddle: http://jsfiddle.net/uqJHf/ I have set the first column to show up with
First of all, do you see red?... http://jsfiddle.net/yaBqr/ I do in IE7 and I
First of all I want to link you guys to the JSFiddle I've made
It's my first jQuery script, very short: http://jsfiddle.net/XGrJR/ in IE 7, when I click
See example http://jsfiddle.net/qqyYU/1/ <div class=detail> <p><b>Entry</b><br> First <span>40,-</span><br> Second <span>20,-</span></p></div> css .detail p span:first-child
Well Im stucked at this point.please take a look at this http://jsfiddle.net/karthik64/5jhgF/3/ Okay first
http://jsfiddle.net/cwUcM/ I've gone through all of the other threads that seems to have a
I did my first knockout.js application http://jsfiddle.net/Pqs7e/ For display application parts (books section, about

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.