-1

I have a html which calls this js file. The html and js work fine in ie9 and above, but for some reason ie8 is not working with this js file, is it possible the html file is not loading the file or is there something wrong with the code?

function countTotalByClassName(klass){
 total=0;
 var arr = document.getElementsByClassName(klass);
 for (var index = 0; index < arr.length; index++) {
     // console.log(arr[index].checked);
     if(arr[index].checked){
     total=total+1;
     }
 };
 return total;
} // END countTotalByClassName()

/* getGreatestChoice()
 *
* Given an array of hashes, return the one with the highest number.
*
*/
function getGreatestChoice(arr){
res="";
size=0;
for (var i = 0; i < arr.length; i++) {
if (arr[i].count > size){
    res=arr[i].name;
}
}
return res;
} // END getGreatestChoice()

 function results(){
 item1 = {}; 
 item2 = {}; 
 item3 = {}; 
 item4 = {}; 

 arr=[];    // array of items

 // BEGIN BUILD HASH ARRAY
 item1["name"]="<h1>Lorem1</h1><p>random text</p><img src='img/Hi.png' class='img-responsive'>";
 item1["count"]=countTotalByClassName("A");
 arr.push(item1);
 item2["name"]="<h1>Lorem2</h1><p>random text</p><img src='img/Hi.png' class='img-responsive'>";
 item2["count"]=countTotalByClassName("B");
 arr.push(item2);
 item3["name"] = "<h1>Lorem3</h1><p>random text</p><img src='img/Hi.png' class='img-responsive'>";
 item3["count"] = countTotalByClassName("C");
 arr.push(item3);
 item4["name"] = "<h1>Lorem4</h1><p>random text</p><img src='img/Hi.png' class='img-responsive'>";
 item4["count"] = countTotalByClassName("D");
 arr.push(item4);
 // END BUILD HASH ARRAY

 res=getGreatestChoice(arr);

 document.getElementById('result').innerHTML = res;
 document.getElementById('result').style.width = '100%';
 document.getElementById('result').style.backgroundColor = '#F0F0F0';
 document.getElementById("result").style.padding="5px 10px 10px 10px"; 

} // END results()
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
rob
  • 291
  • 3
  • 9
  • 21

1 Answers1

2

It seems that the method getElementsByClassName is not supported in IE8. Try using querySelectorAll, that works fine in IE8.

Instead of:

var arr = document.getElementsByClassName(klass);

Use:

var arr = document.querySelectorAll('.' + klass);

If you also need support for IE6, then you can use this polyfill.

lante
  • 7,192
  • 4
  • 37
  • 57