3

This is not a duplicate. Previous questions relate to IE8. This is occurring in IE11.

I have no problems running this in Chrome or Firefox, but my code must function in IE11 and I am receiving the following error:

Object doesn't support property or method 'getElementsByClassName'

function showNext(a) {
    var questions = document.getElementsByClassName("questionholder");
    showRequired.style.display = "none";

    for (var i = 0; i < questions.length; i++) {
        questions[i].style.display = "none";
    }

    var nextQuestion = document.getElementById("question" + a);

    if (nextQuestion !== null) {
        nextQuestion.style.display = "inline-block";
    }
}

The code is supposed to be looking here:

<form id="TheForm" style="display:block;">
    <div class="questionholder" id="question0" style="display:block">
        <a class="text2button" onclick="showNext(1)">Start</a>
    </div>
    <div class="questionholder" id="question1" style="display:block">
        <a class="text2button" onclick="showNext(2)">Q1</a>
    </div>
    <div class="questionholder" id="question2" style="display:block">
        <a class="text2button" onclick="showNext(3)">Q2</a>
    </div>
</form>

The code above will hide all divs and then show the div that matches the id "question"+a as determined by the button clicked.

What can I do to resolve the above error?

Sweepster
  • 1,829
  • 4
  • 27
  • 66
  • 1
    Are you able to show a working example, as IE11 certainly has `document.getElementsByClassName` – Keith May 22 '19 at 21:37
  • Related: [javascript document.getElementsByClassName compatibility with IE](https://stackoverflow.com/q/7410949/1715579) – p.s.w.g May 22 '19 at 21:37
  • 1
    It's not a duplicate, this is not 2014 and IE8, IE9+ has had this method. – Keith May 22 '19 at 21:41
  • 1
    Can't reproduce. My IE11 has `document.getElementsByClassName`. – melpomene May 22 '19 at 21:41
  • 2
    @JackBashford This is not a duplicate of the question you linked to. That question is about IE8, where `.getElementsByClassName()` wasn't implemented. This question is about IE11, where it is. – Scott Marcus May 22 '19 at 21:59
  • I made a test with your above code in IE 11 and this is the output. https://i.postimg.cc/Qd3GQDmW/35.gif You can see that your code is working fine in IE 11. So it looks like your above code does not have any issue. It can be possible that some other code in your web page causing this issue. – Deepak-MSFT May 23 '19 at 01:45

1 Answers1

3

The fix is as follows:

<meta http-equiv="X-UA-Compatible" content="IE=11" />
Sweepster
  • 1,829
  • 4
  • 27
  • 66