vendredi 13 août 2021

How can I change the background color of element in an array

I have an array of 5 divs

`<div id="cap-left2"</div>`
`<div id="cap-left1"</div>`
`<div id="cap-base"</div>`
`<div id="cap-right1"></div>`
`<div id="cap-right2"</div>`

all these divs have a background . In my javascript I have :

let items = [capBase,capLeft1,capLeft2,capRight1,capRight2];

this works :

`var tom = items[Math.floor(Math.random()*items.length)]
console.log(tom)`

and this works

`var tom = items[Math.floor(Math.random()*items.length)]`
`console.log(tom.style)`

but I want the backgroundColor and neither of these work:

`var tom = items[Math.floor(Math.random()*items.length)]`
`console.log(tom.style.background)`


`var tom = items[Math.floor(Math.random()*items.length)]`
`console.log(tom.style.backgroundColor)`

what I am trying to do is lets say i have 5 swatches represented by 5 elements in an array . i want to be able to have a button that allows me to randomize what colors fill each element

any help would be appreciated




Aucun commentaire:

Enregistrer un commentaire