0

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.

user3526204
  • 509
  • 5
  • 22
  • 2
    What version is your IE? `getElementsByClassName` only works with IE9 and higher. – Nope Nov 29 '16 at 12:41
  • Ohk...so it would be great if you could tell me an alternate to this. – user3526204 Nov 29 '16 at 12:43
  • 1
    @user3526204 Check out the other linked question. They do mention `querySelector` and `querySelectorAll` there in the answers of alternative options too. Seeing you use IE8, `querySelector` / `querySelectorAll` should be an option as it works with IE8 and up. – Nope Nov 29 '16 at 12:45
  • Thanks a lot ...will have to redo the carousel I guess. Eitherways I am not very familiar with Js. Wish me luck guys. Thanks a ton everybody ! – user3526204 Nov 29 '16 at 12:50

2 Answers2

1

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

Community
  • 1
  • 1
Ali Bahrami
  • 5,935
  • 3
  • 34
  • 53
1

Old IE doesnt support getByClassName.

You can use below code to as turnaround for this

function getElementsByClassName(className) { var found = []; var elements = document.getElementsByTagName("*"); for (var i = 0; i < elements.length; i++) { var names = elements[i].className.split(' '); for (var j = 0; j < names.length; j++) { if (names[j] == className) found.push(elements[i]); } } return found; }

  • Thanks a lot Rupesh. Just one more thing. How can I get the variable x from this. Sorry I sound amateur. But I am that's y... – user3526204 Nov 29 '16 at 13:11
  • Hey I got that too. And now my slider is working fine on ie 8 ! Thanks a ton for the solution @Rupesh Dhadiwal – user3526204 Nov 29 '16 at 14:05