I’ve got a dropdown menu that I’m having trouble to get to work in Internet Explorer. It’s working fine with Chrome and Firefox but it doesn’t do anything in Internet Explorer. And I prefer to keep all my javascript in my separate document javascript.js as well as I don’t want to work with a library.
The HTML code I’m using is this:
<div id="sidemeny-container">
<div class="sidemenu-maincat">
<img src="cat1.jpg" alt="cat1" />
<div class="sidemenu-subcat hidden">
<a href="subcat1.html"> - Subcat 1 </a>
</div>
<div class="sidemenu-subcat hidden">
<a href="subcat2.html"> - Subcat 2 </a>
</div>
<div class="sidemenu-subcat hidden">
<a href="subcat3.html"> - Subcat 3 </a>
</div>
</div>
<div class="sidemenu-maincat">
<img src="cat2.jpg" alt="cat2" />
<div class="sidemenu-subcat hidden">
<a href="subcat2-1.html"> - Subcat 2-1 </a>
</div>
<div class="sidemenu-subcat hidden">
<a href="subcat2-2.html"> - Subcat 2-2 </a>
</div>
<div class="sidemenu-subcat hidden">
<a href="subcat2-3.html"> - Subcat 2-3 </a>
</div>
</div>
</div>
And the CSS code:
#sidemeny-container {
border-bottom:1px #000 solid;
height: auto;
width: 153px;
float:left;
padding: 10px 0px 0px 0px;
}
.sidemenu-maincat {
border-top: 1px #000 solid;
border-right: 1px #000000 solid;
width:148px;
height:auto;
float:left;
padding: 0px 0px 0px 5px;
}
.sidemenu-subcat.hidden {
display:none;
width:148px;
height:auto;
float:left;
padding: 0px 0px 0px 15px;
}
And the javascript, which I have in a separate .js document:
function initiate()
{
if (document.getElementsByClassName)
{
var sideMenuOptions = document.getElementsByClassName('sidemenu-maincat');
for (var i = 0; i < sideMenuOptions.length; i++) {
sideMenuOptions[i].addEventListener('click', function () {
var subMenuItems = this.getElementsByClassName('sidemenu-subcat');
for (var s = 0; s < subMenuItems.length; s++) {
var subItem = subMenuItems[s];
if (subItem.offsetWidth === 0 && subItem.offsetHeight === 0) {
subItem.className = 'sidemenu-subcat';
} else {
subItem.className = subItem.className + ' hidden';
}
}
});
}
}
else
{
var sideMenuOptions = document.getElementsByTagName('div');
for (var i = 0; i < sideMenuOptions.length; i++) {
if (sideMenuOptions[i].className == 'sidemenu-maincat')
{
sideMenuOptions[i].addEventListener('click', function () {
var subMenuItems = this.getElementsByClassName('sidemenu-subcat');
for (var s = 0; s < subMenuItems.length; s++) {
var subItem = subMenuItems[s];
if (subItem.offsetWidth === 0 && subItem.offsetHeight === 0) {
subItem.className = 'sidemenu-subcat';
} else {
subItem.className = subItem.className + ' hidden';
}
}
});
}
}
}
}
window.onload = initiate;
Your code works fine on IE9 & IE10. However, previous versions do not support
addEventListener, and theattachEventmethod does not supply acurrentTargetproperty. For this reason, the only way to determine the calling object is to replace thethisreference via prototyping (or use a framework).Another problem with your code is that IE8 does not support
getElementsByClassName(). While your code tests for this, the fallback tries to use it again to instantiatesubMenuItems. A better approach would be to use Document.querySelectorAll, which works in IE8 and up and would allow you to avoid duplicated code.Complete example: