Why do all my buttons turn green when I use coloredButton(function,color) function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why do all my buttons turn green when I use coloredButton(function,color) function?

When I use the following code, all buttons are green. Why? and how do I fix this? code: ``` var coloredButton = function coloredButton(func, color="white") { this.func = func; this.color = color; document.write("<style>input {background-color:"+color+"; color:"+color+"; font-size:200%;}</style>"); document.write("<input type=\"button\" value=\"----------\" onclick=\""+func+"\"/>"); } var BA = new coloredButton("alt1()","red"); var BB = new coloredButton("alt2()","blue"); document.write("<br/>"); var BC = new coloredButton("alt3()","yellow"); var BD = new coloredButton("alt4()","green"); function alt1() { alert("hi"); } function alt2() { alert("bye"); } function alt3() { alert("enter"); } function alt4() { alert("exit"); } ```

21st May 2019, 5:42 PM
CALIN BAENEN
CALIN BAENEN - avatar
1 Answer
+ 3
I think it's happening because last style overrides previous styles' properties It works correctly when you write it like this: document.write('<input style="background-color:' + color + '; color:' + color + '; font-size:200%;" type="button" value="----------" onclick="' + func + '"/>');
21st May 2019, 6:12 PM
Mert Yazıcı
Mert Yazıcı - avatar