- 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
4 Answers
+ 6
document.getElementById("you").style.color = green;
+ 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";
+ 1
color is not an attribute but a style property!
...i believe Louis's answer solves this.
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>