Updated on Kisan Patel
This tutorial show you how to set the CSS style to the html element using JavaScript.
To set the CSS style using JavaScript, style followed by CSS property name can be used.
<script type="text/javascript" language="javascript"> function TestFunction() { document.getElementById("divText").style.background = "red"; } </script> <label id="divText">Hello World!</label> <input type="button" id="btnTest" onclick="TestFunction()" value="Click me" />
In the above code snippet, we have found out the label using its id and then setting its background color to “red” using the style.background
property.
See the Pen PqLyLL by Kisan (@pka246) on CodePen.