What is the proper and fastest way to change CSS values using JavaScript? For example, if I have a style.css file:
#h1 {
color: 'red';
}
I need to change the color to any other color and update the CSS file.
What is the proper and fastest way to change CSS values using JavaScript? For example, if I have a style.css file:
#h1 {
color: 'red';
}
I need to change the color to any other color and update the CSS file.
JS:
document.getElementById("elementsId").style.color= "red";
My recommendation would be not to use Id name like h1 as it may be confusing with the <h1> tag in html. Use more clear variable name like headerId.
for changing multiple css properties use this:
document.getElementById(elementsId).setAttribute("style","width: 500px; background-color: yellow;");
document.getElementById(elementsId).setAttribute("style","width: 500px; background-color: yellow;"); or you can write a class in css in advance and add that class to your element when needed using Js as pointed by prashant