Javascript this.firstChild .nextSibling Not Working in Internet Explorer

4.9k Views Asked by At

In the following markup, the "tip1"'s visibility is supposed to change from 'hidden' to show when the parent is moused over. Then when "tip1" is clicked, "line1" is supposed to appear. It works fine in Firefox and Chrome but, of course, not in IE.

<div id="product-description" style="position:relative; float:left; width:35%">
   <div onmouseover="display(this.firstChild)" onmouseout="getRid(this.firstChild)" style="position:absolute; left:146px; top:29px; z-index:2000">
   <div id="tip1" onclick="showTip(this.nextSibling)">
      <img "shadow.png" />
   </div>
   <div id="line1" style="position:absolute; left:15px; top:-5px;" onclick="closeTip(this)">
      <img "fb.png" />
   </div>
</div>
</div>

And here is the corresponding javascript:

<script>
function display(items){items.style.visibility = "visible";}
function getRid(items){items.style.visibility = "hidden";}
function showTip(tip){tip.style.visibility = "visible";}
function closeTip(tip){tip.style.visibility = "hidden";}
</script>
2

There are 2 best solutions below

1
On

Your code won't work in any modern browser. firstChild returns the first node in an element. This node can also be a textnode, in your case it's a new line + TAB. Textnodes don't have style to set, hence the code will fail.

Use firstElementChild instead.

The same stands for nextSibling, use nextElementSibling instead.

A live demo at jsFiddle.

0
On

This turned out to be an issue of z-indexing on IE. I had an image under the hover s which for whatever reason kept covering up my hover buttons. So I removed that image and created another div with a background-image.