Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code works fine on IE9 &amp; IE10. However, previous versions do not support <code>addEventListener</code>, and the <code>attachEvent</code> method <a href="http://www.quirksmode.org/js/events_order.html" rel="nofollow noreferrer">does not supply a <code>currentTarget</code> property</a>. For this reason, the only way to determine the calling object is to replace the <code>this</code> reference <a href="https://stackoverflow.com/a/4590302/1170489">via prototyping</a> (or use a framework).</p> <p>Another problem with your code is that IE8 does not support <code>getElementsByClassName()</code>. While your code tests for this, the fallback tries to use it again to instantiate <code>subMenuItems</code>. A better approach would be to use <a href="https://developer.mozilla.org/en-US/docs/DOM/Document.querySelectorAll" rel="nofollow noreferrer">Document.querySelectorAll</a>, which works in IE8 and up and would allow you to avoid duplicated code.</p> <p>Complete example:</p> <pre><code>Element.prototype.addAnEvent = function(name, funct) { if (this.addEventListener) { this.addEventListener(name, funct, false); } else if (this.attachEvent) { var _this = this; this.attachEvent("on" + name, function() { funct.apply(_this); // where the value of "this" in funct should point to "element" }); } }; function initiate() { var sideMenuOptions = document.querySelectorAll('.sidemenu-maincat'); for (var i = 0; i &lt; sideMenuOptions.length; i++) { sideMenuOptions[i].addAnEvent('click', function() { openSubmenu(this); }); } } function openSubmenu(element) { var subMenuItems = element.querySelectorAll('.sidemenu-subcat'); for (var s = 0; s &lt; subMenuItems.length; s++) { var subItem = subMenuItems[s]; if (subItem.offsetWidth === 0 &amp;&amp; subItem.offsetHeight === 0) { subItem.className = 'sidemenu-subcat'; } else { subItem.className = subItem.className + ' hidden'; } } } window.onload = initiate; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload