Reputation: 155
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.
Upvotes: 0
Views: 2121
Reputation: 1874
$('h1').css('color','#ff5722');
#h1 {
color: 'red';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1> this color tho</h1>
Jquery is from javascript so once you learn jquery you will sometimes go back to javascript
Upvotes: 1
Reputation: 1521
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;");
Upvotes: 3
Reputation: 22490
for multiple css property change use with classname
add .it reduce the code lines in dom
document.querySelector('#h1').classList.add('your-class')
Upvotes: 3