+2  A: 

I don't have IE 7 installed, but it was the same issue with IE 6. Here's what I did to fix it:

$(function() {
  $('#click').click(function() {
    $(".compact th+th,.compact td+td").toggleClass('junk',this.checked);
  });
});

The problem was with you selector. Toggling on compact would not add visibility to junk.

Gert G
I don't think that's true. In the page that *works*, the CSS is exactly the same, and you can toggle from visible to invisible and back again all you like. It's kind-of fun. However, when things start off such that the content is *hidden*, then things do not work.
Pointy
Well, obviously IE 6 and IE 7 has some issues. Anyway, My code above works in IE 6 and FF.
Gert G
Yes, I'm sure you're right @Gert G.
Pointy
+2  A: 

This is a rendering bug. IE6/7 doesn't use a proper table display model. Unfortunately I can't recall a specific name/label for this particular bug and I can't seem to find authorative resources confirming this.

At least, I found 2 CSS ways to fix this.

  1. The easiest, use visibility: hidden; instead of display: none;. Unfortunately this doesn't work nicely if you have more columns after the to-be-toggled column or have a table border. It still leaves a space. Adding position: absolute; to .junk fixes this issue in FF, but you fall back to the same rendering problem in IE.

  2. A hack which abuses the IE's erroneous ability to apply styles for <col>.

    <!DOCTYPE html>
    <html>
        <head>
            <script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
            <script>
                $(function() {
                    $('#click').click(function() {
                        $('table').toggleClass('compact', this.checked);
                    });
                });
            </script>
            <style>
                table.compact .junk { display: none; }
            </style>
            <!--[if lte IE 7]>
            <style>
                table.compact .junk { display: block; }
                table.compact col.junk { display: none; }
            </style>
            <![endif]-->
        </head>
        <body>
            <input type="checkbox" id="click" checked>
            <table class="compact">
                <col />
                <col class="junk" />
                <tr>
                    <th>Hello</th>
                    <th class="junk">World</th>
                </tr>
                <tr>
                    <td>Foo</td>
                    <td class="junk">Bar</td>
                </tr>
                <tr>
                    <td>Foo</td>
                    <td class="junk">Bar</td>
                </tr>
            </table>
        </body>
    </html>
    

Alternatively, you can also just toggle the elements of actual interest in jQuery:

$(function() {
    $('#click').click(function() {
        $('table.compact .junk').toggle(!this.checked);
    });
});
BalusC
I played around a bit more with Pointy's code and came to the same conclusion... changing `display: none;` to `visibility:hidden;` makes it work. Good post (+1).
Gert G
Well the whole point is to keep the table width under control, so "hidden" won't really do it. However, I will definitely try that "col" thing; I've never seen it before! Thanks @BalusC!! You are like some sort of browser ninja
Pointy
You're welcome.
BalusC