In case we need to set the CSS style to the html element using JavaScript, we can follow this approach.
To set the CSS style using JavaScript, we use style property followed by CSS property name.
<script> function TestFunction() { document.getElementById("lblText").style.background = "red"; } </script> <label id="lblText">0</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.
Above function changes the background color of “lblText” to red.
OUTPUT
Views: 5336 | Post Order: 61