javascript css add code example
Example 1: add style javascript
// Bad:
element.setAttribute("style", "background-color: red;");
// Good:
element.style.backgroundColor = "red";
Example 2: how to give css style in javascript
document.getElementById("myH1").style.color = "red";
Example 3: add css in javascript
document.getElementById("demo").style.display = "none";
Example 4: add css style sheet with javascript
<head>
<script>
function myFunction() {
var x = document.createElement("LINK");
x.setAttribute("rel", "stylesheet");
x.setAttribute("type", "text/css");
x.setAttribute("href", "styles.css");
document.head.appendChild(x);
}
myFunction()
</script>
</head>