The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript random color js

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • #168013

    I would like to change the script that I found that it changes the background color of a div, not the body background color.

    The script is:
    var bgcolorlist=new Array(“#DFDFFF”, “#FFFFBF”, “#80FF80”, “#EAEAFF”, “#C9FFA8”, “#F7F7F7”, “#FFFFFF”, “#DDDD00”)[Math.floor(Math.random()*bgcolorlist.length)]

    I would like to change it so it is randomising the background of color-1.

    Here is my HTML:

    <section class=”color-1″>

    <button class=”btn btn-1 btn-1b”>About the author</button>


    I do not know enough javascript so I get an error but I think it should be some thing like:

    var bgcolorlist=new Array(“#DFDFFF”, “#FFFFBF”, “#80FF80”, “#EAEAFF”, “#C9FFA8”, “#F7F7F7”, “#FFFFFF”, “#DDDD00”)


    Any help is appreciated.
    Thank you.


    Did a little bit of research on what your talking about and i think this might be what you looking for:(note this randomly changes the background to something in the list every time the page is refreshed)

    $(document).ready(function () {
    var back = ["#ff0000","blue","gray"];   /*This is your list of colors, list as many as you want */
    var rand = back[Math.floor(Math.random() * back.length)];
    $('/*css selector of item you want to randomly change*/').css('background',rand);

    I don’t know how much jquery you know so i have stuff in comments to help.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.