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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:02:53+00:00 2026-06-03T18:02:53+00:00

The script below applies random colours from an array to a div id, and

  • 0

The script below applies random colours from an array to a div id, and changes the colours on hover. I would like to amend it so that it loops through a div class and applies random colours to all classes on a page. Obviously the click functions would have to be done by id, but I imagine there is a way of looping through the div classes such that they all have different random colours which change on hover.

Could someone help with this?

Thanks

Nick

$(document).ready(function() {
  var test = $("#example").text().split('');

    var normal = generateColors(test);
    var hover = generateColors(test);
    $("#example").html(normal);

    $("#example").hover( 
      function(event) { $("#example").html(hover) }, 
      function(event) { $("#example").html(normal) });

    $("#example").click(function() { 
    location.href = "http://www.google.co.uk"; 
    });

});

function generateColors(characters) {
    var result = "";
    var i = 0;
    for(i=0; i < characters.length; i++) {
        result += "<span style='color:"+getColor()+"'>"+characters[i]+"</span>";
    }

   return result;
}        

function getColor() {
    var colList = ['#7EA404', '#14AFB0','#B05718', '#B0A914', '#B01617','#902BB0', '#B003A2', '#4A429C','#33821E', '#226795', '#D0B600','#886833'];

    var i = Math.floor((Math.random()*colList.length));
  return colList[i];
}
  • 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-03T18:02:57+00:00Added an answer on June 3, 2026 at 6:02 pm

    To apply the colors, and store them on the different elements, you could do:

    $(".example").each(function() {
        var text = $(this).text().split(''),
            normal = generateColors(text),
            hover = generateColors(text);
    
        $(this).html(normal).data('hover', hover).data('normal', normal);
    });
    
    $(".example").hover( 
        function() {
            $(this).html($(this).data('hover'));
        }, 
        function() { 
            $(this).html($(this).data('normal'));
    });
    

    Setting each link with location.href on click seems like a really bad idea, but up to you.

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

Sidebar

Related Questions

That script below is for uploading an image via PHP. Now I'ld like to
In the script below, the reset button acts also like a submit button. Where
The script below adds items to an array when you click the link, and
I have a ruby script below which infinitely prints numbers from 1 onward. How
Another newbie question... The script (below) compares selected text in one div with some
I am using this script below and it works well except that it is
I'm using the script below to refresh a div when the text 'view more'
The script below, test.php, is intended to be placed in a specific directory of
The script below resides in my theme's functions.php file. It is designed to show
The script below worked on my Mac OS X. I'm now using Ubuntu OS,

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.