- This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
Viewing 2 posts - 1 through 2 (of 2 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › random color js
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:
<script>
var bgcolorlist=new Array(“#DFDFFF”, “#FFFFBF”, “#80FF80”, “#EAEAFF”, “#C9FFA8”, “#F7F7F7”, “#FFFFFF”, “#DDDD00”)
document.body.style.background=bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)]
</script>
I would like to change it so it is randomising the background of color-1.
Here is my HTML:
<button class=”btn btn-1 btn-1b”>About the author</button>
</section>
I do not know enough javascript so I get an error but I think it should be some thing like:
<script>
var bgcolorlist=new Array(“#DFDFFF”, “#FFFFBF”, “#80FF80”, “#EAEAFF”, “#C9FFA8”, “#F7F7F7”, “#FFFFFF”, “#DDDD00”)
document.color-1.background=bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)]
</script>
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)];
console.log(rand);
$('/*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.