Using javascript to change rgb value of background onclick

background-color, javascript

Solution

You have wrapped it in `'` .. why ?

If you remove that it works..

var thergb = "rgb(" + x + "," + y + "," + z + ")"; 

Demo at http://jsfiddle.net/gaby/L92bY/9/

(you also needed to define the `change` function in the `head` tag and not in the `onLoad` event..)

Problem

I'm trying to implement a very simple JavaScript program: every time you click the button, the RGB values of the background color are randomized. Here's the Javascript: ``` function change() { var x = Math.floor(Math.random() * 256); // range is 0-255 var y = Math.floor(Math.random() * 256); var z = Math.floor(Math.random() * 256); var thergb = "'rgb(" + x + "," + y + "," + z + ")'"; console.log(thergb); document.body.style.background=thergb; } ``` I'm pretty sure the problem is in how I hack together the `thergb` variable, but there are no errors in the console so I'm not quite sure. I log the console just to make sure it's giving me an actual random rgb, which it is. Here's the full JSFiddle: http://jsfiddle.net/L92bY/

Original source