palmsey palmsey - 8 months ago 47
CSS Question

style.display not working in Firefox, Opera, Safari - IE7 OK

I have an absolutely positioned div that I want to show when the user clicks a link. The onclick of the link calls a js function that sets the display of the div to block (also tried: "", inline, table-cell, inline-table, etc). This works great in IE7, not at all in every other browser I've tried (FF2, FF3, Opera 9.5, Safari).

I've tried adding alerts before and after the call, and they show that the display has changed from "none" to "block" but the div does not display.

I can get the div to display in FF3 if I change the display value using Firebug's HTML inspector (but not by running javascript through Firebug's console) - so I know it's not just showing up off screen, etc.

I've tried everything I can think of, including:

  • Using a different doctype (XHTML 1, HTML 4, etc)

  • Using visibility visible/hidden instead of display block/none

  • Using inline javascript instead of a function call

  • Testing from different machines

Any ideas about what could cause this?

Answer Source

Since setting the properties with javascript never seemed to work, but setting using Firebug's inspect did, I started to suspect that the javascript ID selector was broken - maybe there were multiple items in the DOM with the same ID? The source didn't show that there were, but looping through all divs using javascript I found that that was the case. Here's the function I ended up using to show the popup:

function openPopup(popupID)
  var divs = getObjectsByTagAndClass('div','popupDiv');
  if (divs != undefined && divs != null)
    for (var i = 0; i < divs.length; i++)
      if (divs[i].id == popupID)
        divs[i].style.display = 'block';        

(utility function getObjectsByTagAndClass not listed)

Ideally I'll find out why the same item is being inserted multiple times, but I don't have control over the rendering platform, just its inputs.

So when debugging issues like this, remember to check for duplicate IDs in the DOM, which can break getElementById.

To everyone who answered, thanks for your help!