JavaScript > Dealing with HTML Controls

Setting CSS style to HTML element in JavaScript

How to set CSS style to html element using JavaScript?

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.

        function TestFunction() {
            document.getElementById("lblText").style.background = "red";

    <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.


 Views: 746 | Post Order: 61

Write for us