Reputation: 77
This code is perfectly running in Google chrome; However, it is not working in Internet Explorer.
function HideMyClassItems()
{
var classone= document.getElementsByClassName('classone');
for(var i=0; i<classone.length; i++) {
classone[i].style.display='none';
}
}
What's wrong?!
Upvotes: 0
Views: 657
Reputation: 525
IE8 and under doesn't support getElementsByClassName()
, but there's three options you can try.
1: You could create a function
function getElementsByClassName(node, classname) {
var a = [];
var re = new RegExp('(^| )'+classname+'( |$)');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; i<j; i++)
if(re.test(els[i].className))a.push(els[i]);
return a;
}
And use the function like this
var classone = getElementsByClassName(document.body,'classone'); // no document
2: You can use jQuery.
var classone = $('.classone');
3: You can use the querySelectorAll()
method/function.
var classone = querySelectorAll('.classone');
Upvotes: 2
Reputation: 10158
In case of Internet Explorer, getElementsByClassName
method is supported since version 9.
Here's the reference: http://caniuse.com/getelementsbyclassname
Upvotes: 0
Reputation: 3445
getElementsByClassName
method might not be available in all of the browsers.
You can try out jQuery
library for a cross-browser selector solution.
Upvotes: 0