-1

I have tried to use this (I know that ID should not be the same, but it must not be different in IE):

document.getElementById("myid")

but it only give me one element, but I need to count the length of the elements which are 176. Do not ask me why; this is the requirement.

I have to let my project run for IE version 5 to 11 and Edge.

TylerH
  • 20,799
  • 66
  • 75
  • 101
BKM
  • 186
  • 2
  • 15
  • 1
    Poor style of code, just give every element to be selected the same class, that should work for Edge. Another suggestion is to use jquery, it also works with Edge. – Aaron Nov 22 '19 at 12:05
  • class wount work with internet explorer below 9 – BKM Nov 22 '19 at 12:06
  • 1
    Possible duplicate of [javascript document.getElementsByClassName compatibility with IE](https://stackoverflow.com/questions/7410949/javascript-document-getelementsbyclassname-compatibility-with-ie) – TylerH Nov 22 '19 at 14:43

1 Answers1

1

From the getElementById() method, getElementsByClassName() method and the querySelectorAll document, we can see that: The getElementById() method support IE5.5+, the getElementsByClassName() method support IE9+, and the querySelectorAll() method support IE8+.

To find multiple elements in the old IE browser, you could use the getElementsByName or getElementsByTagName method to find these elements.

The getElementsByName and getElementsByTagName method support IE5+.

please refer to the following sample code:

<div id="content">
    <input type="text" id="txtvalue" name="inputvalue" value="item 1" /><br />
    <input type="text" id="txtvalue" name="inputvalue" value="item 2" /><br />
    <input type="text" id="txtvalue" name="inputvalue" value="item 3" /><br />
    <input type="text" id="txtvalue" name="inputvalue" value="item 4" /><br />
    <input type="text" id="txtvalue" name="inputvalue" value="item 5" /><br />
</div>

<script> 
    var items = document.getElementsByName("inputvalue"); 
    console.log(items.length);  //output: 5
    var items2 = document.getElementById("content").getElementsByTagName("input"); 
    console.log(items2.length); //output: 5
</script>
Zhi Lv
  • 18,845
  • 1
  • 19
  • 30