2

Why this function getElementsByClassName not work on IE7 ,8 ?

i think cause my issue from

var decimal_id_name = document.getElementsByClassName('decimal')[z_decimal].id

How to apply for work on IE7, 8 ?

http://jsfiddle.net/Fp4sJ/828/

To test , When user press button, It's will alert id name of input.

<form method="post" ENCTYPE = "multipart/form-data" onsubmit="return checkform(this);">
<input type="text" class="decimal" id="number0" size="20" name="number[]">
<input type="text" class="decimal" id="number1" size="20" name="number[]">
<input type="text" class="decimal" id="number2" size="20" name="number[]">
<input type="text" class="decimal" id="number3" size="20" name="number[]">
<input type="text" class="decimal" id="number4" size="20" name="number[]">
<input type="submit" name="submit" value="Next"/>
</form>

<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
    function getElementsByClassName(node, classname) {
        var a = [];
        var re = new RegExp('(^| )'+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;
    }
    var list_decimal = getElementsByClassName(document.body,'decimal');


    for (z_decimal = 0; z_decimal < list_decimal.length; ++z_decimal) {
        var decimal_id_name = document.getElementsByClassName('decimal')[z_decimal].id
        alert(decimal_id_name);
    }
    return false ;
}
</script>
Sundar Rajan
  • 133
  • 1
  • 1
  • 12

3 Answers3

0

This function is not supported in IE7-8

http://caniuse.com/#search=getElementsByClassName

You should try to polyfill it

https://gist.github.com/eikes/2299607

strapro
  • 153
  • 8
0

As you can see here http://caniuse.com/#search=getElementsByClassName and here https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByClassName function getElementsByClassName was introduced in IE9, so it won't work in earlier versions.

wgslr
  • 76
  • 1
  • 4
0

getElementsByClassName is not supported by IE8, only IE9 and above. You can use polyfills to get the functionality, or you can use document.querySelectorAll(selector) which is a much more robust function. The parameter selector is anything the IE8's CSS selector can do.

Zenorbi
  • 2,504
  • 2
  • 15
  • 18