Knowledge Walls
John Peter
Pune, Maharashtra, India
How to use css styles using javascript with Example
2814 Views
Style property 
style is a property of element object or node object in javascript.

Example
document.getElementById("id_name").style.color = "red";
Example
<html>
    <head>
        <title>getElementsByName example</title>
        <script type="text/javascript">
            function applyCSSStyle(className,cssname,cssvalue){
                var elements = document.getElementsByClassName(className);
                for (var index in elements){
                   if (!isNaN(index))
                   elements[index].style[cssname] = cssvalue;
                }
            }
            window.onload = function(){
                applyCSSStyle("go_green","color","green");
                applyCSSStyle("go_background_red","backgroundColor","red");
            };
        </script>
    </head>
    <body>
        <div class="go_green">
           Green Color
        </div>
        <div class="go_green go_background_red">
           Red background Color with green text
        </div>
    </body>
</html>
Demo & Output 
Best Lessons of "Good Javascript Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details