getElementsByClassName & IE8: Object doesn't support this property or method [duplicate]

Use document.querySelectorAll('.verdana14.toAdd').

See also my related blog post.


getElementsByClassName method is not supported by IE8.

You should use document.querySelectorAll('.classname') (works in IE8+) or a library that implements that functionality - like:

  • jQuery

  • Moo Tools

  • DOJO

  • YUI

  • Prototype

    ... Among others...


querySelectorAll support:

http://www.quirksmode.org/dom/w3c_core.html#t13

getElementsByClassName support:

http://www.quirksmode.org/dom/w3c_core.html#t11


You could write your own. Something like:

function GEBCN(cn){
    if(document.getElementsByClassName) // Returns NodeList here
        return document.getElementsByClassName(cn);

    cn = cn.replace(/ *$/, '');

    if(document.querySelectorAll) // Returns NodeList here
        return document.querySelectorAll((' ' + cn).replace(/ +/g, '.'));

    cn = cn.replace(/^ */, '');

    var classes = cn.split(/ +/), clength = classes.length;
    var els = document.getElementsByTagName('*'), elength = els.length;
    var results = [];
    var i, j, match;

    for(i = 0; i < elength; i++){
        match = true;
        for(j = clength; j--;)
            if(!RegExp(' ' + classes[j] + ' ').test(' ' + els[i].className + ' '))
                match = false;
        if(match)
            results.push(els[i]);
    }

    // Returns Array here
    return results;
}

Will work pretty well, but you could write a faster one if you want to. Then you can just change:

document.getElementsByClassName('verdana14 toAdd');

To:

GEBCN('verdana14 toAdd');

use jQuery, or filter the results from getElementsByTag