+ 1

How do I set a color in jQuery using variable?

I made a random generator of RGB color code. The value of a variable containing this code is a string and it looks something like this: 'rgb(123, 234, 65)'. Let's call it 'x'. I try to use it to set a background-color in jQuery: $('div').css('background-color', x) But it doesn't work. What's a problem?

8th Oct 2017, 4:42 PM
Mikhail Sladkomedov
Mikhail Sladkomedov - avatar
2 Answers
+ 5
Do you have a defined dimension (height and width) of div , or some content inside <div></div>? If you have, how you call this change of background color? With some button, or with $(document).ready(function(){}); ? Anyway, your line : $('div').css('background-color', x); is correct to change background-color of div.
9th Oct 2017, 11:09 AM
LukArToDo
LukArToDo - avatar
+ 1
thanks, I found a mistake in my code and now it works well. it is a random color generator. https://code.sololearn.com/W1kYx6w2bdu6/?ref=app
9th Oct 2017, 11:13 AM
Mikhail Sladkomedov
Mikhail Sladkomedov - avatar