+ 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!

21st Dec 2017, 12:54 AM
Black Frost
Black Frost - avatar
6 Answers
+ 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
21st Dec 2017, 1:07 AM
voidneo
+ 2
set public, I forgot the last word
21st Dec 2017, 3:53 AM
voidneo
+ 1
set
21st Dec 2017, 3:50 AM
voidneo
0
ser?
21st Dec 2017, 3:45 AM
Black Frost
Black Frost - avatar
0
oh ok and by set my code what do you mean?
21st Dec 2017, 3:51 AM
Black Frost
Black Frost - avatar
0
oh. i get what you're saying now give me a second.
21st Dec 2017, 3:56 AM
Black Frost
Black Frost - avatar