Thursday, August 25, 2011

[Javascript] Dynamic class not working in IE7

We all known how to change class for a DIV by Javascript. For example:

document.getElementById("div_name").setAttribute("class", "class_name");

Generally, it works across different browser nowadays. Unfortunately, some users still using IE7 or even older-version, and we have to take care of them. Here is a JS function for workaround.


function rollStyle (styleObj, styleName) {
    if (styleObj.className) {
        styleObj.className = styleName;
    } else {
        document.getElementById(styleObj).className = styleName;
    }
}

 Usage:

rollStyle("div_name","class_name")
Related Posts Plugin for WordPress, Blogger...