- 1

How do I change html text and color with JavaScript

<button onclick="f()"> click me </button> <p id="you"> you good </p> function f() { document.getElementById("you").innerHTML = "lol"; document.getElementById("you").element.setAttribute("style", "color: green;"); } It changes to lol but won’t show green

31st Mar 2018, 5:56 PM
klisco1
4 Answers
+ 6
document.getElementById("you").style.color = green;
31st Mar 2018, 6:01 PM
Louis
+ 5
var elem = document.getElementById("MonitorInformation"); elem.innerHTML = "Setting different HTML content"; elem.style.color = "Red"; elem.style.fontSize = "large"; document.getElementById("myH2").style.color = "#ff0000"; document.getElementById("myP").style.color = "magenta"; document.getElementById("myP2").style.color = "blue"; document.getElementById("myDiv").style.color = "lightblue";
31st Mar 2018, 7:21 PM
Baraa AB
Baraa AB - avatar
+ 1
color is not an attribute but a style property! ...i believe Louis's answer solves this.
31st Mar 2018, 6:37 PM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
0
<!DOCTYPE html> <html> <body> <h2>JavaScript Can Change the HTML Content </h2> <button onclick = "myFunct()"> Click Here!</button> <p id="demo" style="color:red;">Is Javascript is change the HTML Content? </p> <script> function myFunct() { document.getElementById("demo").innerHTML="Yes, The Change the HTML Content."; document.getElementById("demo").style.color = "green"; } </script> </body> </html>
21st Feb 2024, 10:10 PM
Dhivya