Javascript Change Style Class of Div tag

Updated on 23 Mar 2012,
Published on 25 Jun 2008

Javascript Document Object Modeling provides the functionality to change the CSS style class name of the HTML div tag dynamically. Javascript change style class of Div tag function allows you to sets the new class name of the class attribute of Div tag dynamically. You can access the HTML div element using document.getElementById method to change the CSS classname property of the Div tag.

JavaScript DOM Examples:

You can see the live samples and examples of JavaScript DOM from the following links:

Example of Javascript Change Style Class of Div tag

<html>
<head>
    <title>Javascript Change CSS Class of Div tag</title>
    <style type="text/css">
        .redText, .blueText {
            width: 200px;
            font-family: Arial;
        }
        
        .redText {
            color: red;
        }
        
        .blueText {
            color: blue;
        }
    </style>
    <script language="javascript" type="text/javascript">
    <![CDATA[
        function changeCssClass(objDivID) {
            if (document.getElementById(objDivID).className == 'redText') {
                document.getElementById(objDivID).className = 'blueText';
            }
            else {
                document.getElementById(objDivID).className = 'redText';
            }
        }
    ]]>    
    </script>
</head>
<body>
    <center>
        <div id="div1" class="redText">
            By default text indside this HTML div tag is red. Javascript function will change
            the CSS class<br />
            of this Div tag and
            <br />
            text color will change to blue dynamically.
        </div>
        <br />
        <input type="button" 
                value="click here" 
                onclick="changeCssClass('div1')" />
    </center>
</body>
</html>

Above Javascript DOM example changes the class of HTML div tag onclick event of input button control of HTML. Javascript change Css Class function takes the parameter as id of the div tag whose style class is to be changed. It performs the toggle function to change the class of Div tag and reverts back to the default class name when user clicks the button second time. You can embed the CSS style code inside the Head section of the HTML document using HTML style tag as shown in the above sample code.

Output:

You can see the output of above discussed codes from the following link:

Change CSS Class of Div Tag

Continue to next tutorial: Javascript Change Background Color Style to learn how to change the background color of HTML Div element dynamically.

4 Responses to "Javascript Change Style Class of Div tag"
Neil
Your example is the perfect one for my use right now. I was wondering what to do if I want to use multiple div's to resize after click. Like let me explain you. I have a content on one side and sidebar on another. I was to have the sidebar hidden on start and just a button showing on header. And the content to fill screen, only when the user clicks on the function I want the content to be resized in accordance with the sidebar on the screen. Like say my width of page is 800px and sidebar with is 200 px. Now when the page loads 800px = content. and sidebar = 0px. I was looking into content=600px and sidebar =200px after i click the button to show sidebar on screen. CHeers! I hope you have an answer to it. Thanks for the code though.
Ezineasp.net
Hi Neil

Here is a complete source code for your guidance. You can try the code provided in the following tutorial along with its live sample:

Expand Collapse Sidebar Navigation using JavaScript dynamically

Hope this will help you.

Cheers!
JavaNoob
I am using this script to swap my classes on click. I have 4 buttons i want to swap but i want the class to swap back after I click a different button. How could I modify this to achieve this?
Sue
Thanks so much for a robust solution which works perfectly across all browsers. As an amateur Javascript dabbler I've been searching for just this - no other solution worked as well!
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers