user3526204 user3526204 - 8 days ago 7
Javascript Question

issue with running js on Internet Explorer

I have this code which ie has a problem with.

var myIndex = 0;
carousel();

function carousel() {
var i;
var x = document.getElementsByClassName("mySlides");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
myIndex++;
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>
<html>


If that has to do anything with it.

Thanks in advance for any help

my site which i am developing is http://www.mylaundrywala.com ...works fine on Chrome and Firefox as I mentioned.

Ali Ali
Answer

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

Document.getElementsByClassName

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