+ 1
Can someone help me with this code:
<button id="button_1" onclick="changeColor('red')">Click Repeatedly!</button> <script language="javascript" type="text/javascript"> function changeColor(x) { var a = document.getElementsByClassName("red"); onclick.a.style.color = "green"; } </script> I have an h1 with every other letter another color using span class red and green. i am trying to have it to where when you click the button the "greens" turn red and "reds" turn green. cant seem to figure it out. any help, please!
6 Respuestas
+ 5
you could do this:
create classes to color the text:
.red { color: red; }
then do this:
var elem = document.getElementById("red");
elem.classList.add("red");
or just do this:
elem.style = "color: red;";
Do this fir each letter you want to colorize inside a function (I'll call it colorizeLetters()), then you add the event:
var btn = document.getElementById("myBtn");
btn.addEventListener("click", colorizeLetters);
and you're done. It would help to ser your code
+ 2
set public, I forgot the last word
+ 1
set
0
ser?
0
oh ok and by set my code what do you mean?
0
oh. i get what you're saying now give me a second.