1

I'm trying to use getElementsByClassName to hide some elements on my page. I used the 1st answer here:

What is an alternative to using getElementByClass for hiding multiple elements?

to write my own code and it works in Chrome and FF but not in IE9. Everything I've read says getElementsByClassName should work in IE9. Rather than put my own code, the code below is the answer from the link above since it's similar to my own and already compact. When I load this into IE9, the buttons do nothing (fine in Chrome and FF). If I put try and catch around the code in the functions, it says object error. Is it supposed to work?

<head>
<script type="text/javascript">
function hideNames()
{
 var list = document.getElementsByClassName("webname");
for (var i = 0; i < list.length; i++) {
    list[i].style.display="none";
}
}

function showNames()
{
var list = document.getElementsByClassName("webname");
for (var i = 0; i < list.length; i++) {
    list[i].style.display="block";
}
}

</script>
</head>
<body>
<p class="webname">Webname</p>
<p class="webname">test</p>
<input type="button" onclick="hideNames()" value="Hide Web Names" />
<input type="button" onclick="showNames()" value="Show Web Names" />
</body>
Community
  • 1
  • 1
user2155341
  • 37
  • 2
  • 6
  • 4
    You realise that showing us the code that you're having problems with (*your code*) would be more useful than showing us the code that you've altered? Incidentally, what do you get from `console.log(document.getElementsByClassName("webname"));` ([assuming you can use `console.log()` in IE 9](http://stackoverflow.com/questions/5472938/does-ie9-support-console-log-and-is-it-a-real-function))? – David says reinstate Monica Sep 13 '13 at 20:15

1 Answers1

5

You're probably in quirks or compatibility mode. IE9 requires standards mode.

Don't forget your <!doctype html> at the top of the HTML document, and perhaps <meta http-equiv="X-UA-Compatible" content="IE=edge" /> in the <head>.

user2736012
  • 3,503
  • 13
  • 13