I’m trying to create a collapsable list in Internet Explorer 8 for the HTML I have:
<li>
<a href="#" onclick="test('node1')">hello</a>
<ul id="node1" class="node" style="display:none">
<li>Sub-item 1</li>
<li>Sub-item 2</li>
</ul>
</li>
<li>
<a href="#" onclick="test('node2')">test</a>
<ul id="node2" class="node" style="display:none">
<li>Sub-item 1</li>
<li>Sub-item 2</li>
</ul>
</li>
in javascript i have
function test2(className, link) {
var e = document.getElementsByClassName(className);
for (var i = 0, len = e.length; i < len; i++) {
e[i].style.display = "none";
}
link.innerHTML = "Expand";
}
I’m using this to call it:
<a href="#" onclick="test2('node', this)">Collapse</a>
Unfortunately, this method is not working in IE8, and neither is querySelectAll. Can someone provide an example how to fix this please?
Here is a quick solution by extending the
Element.prototypeand thedocument:It’s not always, however, the best idea to extend the prototype object, especially with a function named exactly like a non-existent native function. If you want to escape the problems caused by extension of the prototype, use this code:
That way, you can safely use a
getElementsByClassName()function that accepts two arguments:className: the CSS classcontext: the node