JavaScript changes the style of HTML elements and changes the CSS and element attributes

  • 2020-03-29 23:40:48
  • OfStack

Change the HTML style
To change the style of an HTML element, use this syntax:
 
document.getElementById(id).style.property=new style 
<p id="p2">Hello World!</p> 
<script> 
document.getElementById("p2").style.color="blue"; 
</script> 

JS changes element attributes;
 
<script> 
var div = document.getElementByIdx_x('d1'); 
div.setAttribute("class", "abc"); 
</script> 

Related articles: