user3526204 user3526204 - 6 months ago 52
Javascript Question

issue with running js on Internet Explorer

I have this code which ie has a problem with.

var myIndex = 0;

function carousel() {
var i;
var x = document.getElementsByClassName("mySlides");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
if (myIndex > x.length) {myIndex = 1}
x[myIndex-1].style.display = "block";
setTimeout(carousel, 3000); // Change image every 2 seconds

The Developer Tools says the following line is the problem. It doesn't run any JS on my site ! So I guess it is because of this problem. I know this could be very basic stuff...unfortunately I am not JS literate :(

var x = document.getElementsByClassName("mySlides");

The error is

Object doesn't support this property or method

Being an amateur in Js I was so happy about my site doing wonders on Chrome and Firefox - till my happiness crashed as always with ie...

Would be greatly obliged if somebody can help shed some light on this.

By the way my Doctype is declared as

<!DOCTYPE html>

If that has to do anything with it.

Thanks in advance for any help

my site which i am developing is fine on Chrome and Firefox as I mentioned.

Ali Ali

It depends on what Internet Explorer you are testing your code. As you can see IE9 and higher versions support that method. Document.getElementsByClassName() on MDN


Some one has also suggest a workaround here: getElementsByClassName() doesn't work in old Internet Explorers like IE6, IE7, IE8