Generate a Random Color in JavaScript

Your code didn’t work for me so I rewrote it for fun

var elements = Array.prototype.slice.call(document.querySelectorAll("*"))
  , styleElements = ["h1", "a", "div", "section", "p", "ul", "li", "h2", "h3", "h4", "h5", "h6", "article", "aside", "footer", "span"]

elements = elements.filter(function(element){
  if ( styleElements.indexOf(element.nodeName.toLowerCase()) > -1 ) return true
})

var colors = function() {
  elements.forEach(function(element) {
    element.style.backgroundColor="#"+(Math.random()*0xFFFFFF<<0).toString(16)
    element.style.color = '#'+(Math.random()*0xFFFFFF<<0).toString(16)
    element.style.borderColor = '#'+(Math.random()*0xFFFFFF<<0).toString(16)
  })

  window.setTimeout(colors, 1000)
}

colors()
See also  Liquid Hover Effect Button

Leave a Reply