I am trying to switch a series of blocks between “none” and “block” based on the OnMouseOver property and to change the title of the selected list to yellow at the same time. The JavaScript code I have for this is:
function switchCat(cat) {
var uls = document.getElementsByClassName('lower-ul');
var titles = document.getElementsByClassName('lower-cat-title');
for (var i=0;i<uls.length;i++) {
uls[i].style.display = 'none';
titles[i].style.color = 'white';
}
if (cat != -1) {
var wanted = document.getElementById('lower-cat-'+cat);
var wantedTitle = document.getElementById('lower-cat-title-'+cat);
wanted.style.display = 'block';
wantedTitle.style.color = 'yellow';
}
}
It works with Chrome, Opera, and Firefox, however, it does not work with IE. When I test it in IE I get the error “Object doesn’t support this property or method.” Does anyone know what I am doing wrong?
IE unfortunately does not support
document.getElementsByClassName.Add your own support
However, you can add support to this. You can create your own function, such as the following:
(From here.)
Even easier
Even easier, if you want to, you can use the amazing jQuery to select elements by
classname andid:More cleaner!