Blog: Tag Archives: Divs

Javascript – Find/Hide all elements with a certain class name

Posted On:

function ClosePreviousDivs(clsName)

{


    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++)
    	
    {
        //See if the item contains multiple classes
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                  //  retVal.push(elements[i]);
                  
                  elements[i].style.display="none";
            }
        }
        else if(elements[i].className == clsName)
         //single class
           elements[i].style.display="none";
    }
}