Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthoraxlgroove
    • CommentTimeFeb 6th 2009
     permalink
    So I'm trying to hack together a simple javascript that counts the number of named classes and writes that number back into the page. This is what i have so far. Am I close? Doesn't appear to work for me.

    function getElementsByClass(node,searchClass,tag) {
    var classElements = new Array();
    var els = node.getElementsByTagName(tag); // use "*" for all elements
    var elsLen = els.length;
    var pattern = new RegExp("\\b"+searchClass+"\\b");
    for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
    classElements[j] = els[i];
    j++;
    }
    }
    return classElements;
    }
    function sectionNumber() {
    var sectionNum = getElementsByClass(document,'fooClassName','*');
    document.getElementById('classTotals').innerhtml= sectionNum;
    }

    and in the html i have a div named "classTotals"
Add your comments
    Username Password
  • Format comments as (Help)