50

I know "getElementsByClassName" is not support by IE8. Do you know what can I use instead? I am getting annoying by error

"Object doesn't support this property or method".

The HTML code is:

function sumar() {
var elems = document.getElementsByClassName('verdana14 toAdd');
var myLength = elems.length;
total = 0;
for (var i = 0; i < myLength; ++i) {
   if (elems[i].value!="") {
       total += parseInt(elems[i].value,10);
       }
    }

var promedio = total/myLength;
parseFloat(document.getElementById('promediocal').value = promedio.toFixed(2));
}

This the input text that calls the javascript function:

<input name='AE_EA_1_BIV_003_2' type='text' class='verdana14 toAdd' id='AE_EA_1_BIV_003_2' style='width:50px' onChange='sumar()'/>
<input name='AE_EA_1_BIV_003_3' type='text' class='verdana14 toAdd' id='AE_EA_1_BIV_003_3' style='width:50px' onChange='sumar()'/>
<input name='AE_EA_1_BIV_003_4' type='text' class='verdana14 toAdd' id='AE_EA_1_BIV_003_4' style='width:50px' onChange='sumar()'/>
Gaurravs
  • 639
  • 1
  • 15
  • 30
Jesus Ibarra
  • 551
  • 2
  • 5
  • 11
  • jQuery has a great CSS selector implementation (which includes class name searches): http://jquery.com/ – Tim M. Mar 05 '12 at 15:15
  • 3
    I love jQuery too, and for something running IE8 it almost certainly wouldn't be prohibitive overhead, but for pure vanilla javascript querySelectorAll() is best answer, as long as namespaces are not in the picture. – Swanny May 02 '13 at 05:28

5 Answers5

63

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

See also my related blog post.

Marat Tanalin
  • 13,927
  • 1
  • 36
  • 52
15

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

Community
  • 1
  • 1
Alex
  • 34,899
  • 5
  • 77
  • 90
9

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');
Paul
  • 139,544
  • 27
  • 275
  • 264
  • You need to change `cn.replace(' ', '.')` because it will only replace the first space. You'll need a `g`lobal regex that will also consider adjacent spaces to be one. Same with your `cn.split(/ /)`, it should be global, and test for adjacent spaces, and it should include other space characters, like tabs, so `\s` is probably better. –  Mar 05 '12 at 15:35
  • @amnotiam Thanks yeah I got confused by PHP and forgot which one replaces all without the g modifier. I purposefully switched from `\s` to ` ` because I don't think the function will be called with tabs or newlines. I modified it to trim off trailing spaces as well. – Paul Mar 05 '12 at 15:40
  • Yeah, probably a safe bet about the tabs/new lines. –  Mar 05 '12 at 15:45
  • @amnotiam Also there is no need for a `g` flag on cn.split. It wouldn't do anything because split finds all matches anyways. – Paul Mar 05 '12 at 15:45
  • Yes, you're right about the split. –  Mar 05 '12 at 15:46
1

use jQuery, or filter the results from getElementsByTag

Simon Wang
  • 2,843
  • 1
  • 16
  • 32
-3

you may try using jquery as it have alternative class selector to get the elements by there class names. $(".testclass").each... it will give you required elements by class names

Ganesh Bora
  • 1,133
  • 9
  • 17