Skip to content Skip to sidebar Skip to footer

Expanding And Collapsing Folders

I need to get more than one element to toggle open and closed. Right now the function is just selecting the ID, but I'd like to know how to get it to select a class. I thought I co

Solution 1:

var getElementsByClassName = function(node, classname) {
    if (document.getElementsByClassName) { 
        returndocument.getElementsByClassName(classname);
    }
    var a = [];
    var re = newRegExp('(^| )'+classname+'( |$)');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

varToggle = function(){
    var tp = getElementsByClassName(document.documentElement,'toggle');
        for(var i = 0; i < tp.length; i++){
            if(tp[i].style.display=='none')
                tp[i].style.display='block'else
                tp[i].style.display='none'
    }
}

Use getElementsByClassName and then loop through them.

EDIT

Just make sure they have the class toggle as used in my code above.

UPDATE

Added function for IE support (adopted from https://stackoverflow.com/a/7410966/600101).

Post a Comment for "Expanding And Collapsing Folders"