0

I'm working on a little side project where mulitple colours are generated and displayed using javascript. https://mrkwrght.github.io/totallycolours/

my code is currently this :

 var randomColor15 = "000000".replace(/0/g,function(){return (~~

(Math.random()*16)).toString(16);});

$(function() {

$("#colorbox").css({
    backgroundColor:'#' + randomColor
});
$("#colorcode").text("#" + randomColor);

This block is repeat 16 times.There Must be a easier way of doing this.

I also plan to replace the refresh button with a load more button. but I am unsure how this would be done

Marc-Antoine
  • 23
  • 1
  • 1
  • 9
  • How is it repeated, `#colorbox` should be only *one* single element. Anyway, you only generate one single color, and use that same color each time ? – adeneo May 15 '17 at 14:30
  • https://jsfiddle.net/adeneo/fvzrq8dp/ – adeneo May 15 '17 at 14:32

1 Answers1

0

You have to put it inside a function and then call

var randomColor15 = function(){return "#"+"000000".replace(/0/g,function(){return (~~(Math.random()*16)).toString(16)})};
//only test
for(var i=0;i<10;i+=1){
  console.log(randomColor15());
}
alessandrio
  • 4,282
  • 2
  • 29
  • 40