Hey! This is the dev blog for the app named gosnip.
If you need a neat way to organize your code, gosnip, it's free.

This blog talks about the how's and why's behind gosnip and features articles and tutorials on the techniques used.

Careful when changing “style.display” with JS

A client of mine pointed me to a small bug in Internet Explorer that kept his site from working. I was hiding/showing some tables, based on what radio button the user selects. Pretty simple, but since they were tables, I thought I should set style.display to a value like “table” for when I needed to render the table (I used “none” for hiding). Surprised that this does not work on IE? I know I was.
The error both IE6 and IE7 displayed was “Could not get the display property. Invalid argument.” Which is weird, since I’m not getting, I’m only setting.

It appears that IE does not attribute values like “table”, “table-row” and the like to the style.display property. To solve this, you should set it like:

style.display = ”;

That will revert the element to its initial display state, and in consequence any style.dispay you previously set in the script will be overwritten.
Now, this is a great tip and one worth spreading. Here’s another one:

Always hide elements using JavaScript, not CSS.

Instead of defining a css rule to set the element hidden, leave it as is and hide it in the beginning of your script. In this way, when JS is disabled, your elements will still be there.

3 Responses to “Careful when changing “style.display” with JS”

  1. sergei Says:

    Just a quick message to say thank you for your tip on how to fix style.display bug in IE.

  2. Rob Says:

    Thanks for this - it was really bugging me trying to find the solution.

  3. exodusMT Says:

    try {
    document.getElementById(’trABC’).style.display = ‘table-row’;
    }
    catch(iestupidproblem) {
    document.getElementById(’trABC’).style.display = ‘block’;
    }

Leave a Reply