views:

622

answers:

5

I've got few divs on my website - on the load I want some of them to be hidden, some shown. I am hiding the ones I don't want like this:

$(".divToHide").hide();

It works well in Chrome, Firefox, IE8 an Opera... It doesn't in IE6 (I haven't tested on previous version yet...) - when I load the page all the divs are hidden properly. When I click a link that makes one of them visible it appears correctly. The problems shows when I click on a different link that's supposed to hide the first div and show another. The text of the first div is hidden, but the image stays and obstructs the newly shown div. I'm pretty sure it's a bug - when I zoom in or out of the page the divs that were supposed to be hidden disappear suddenly - they're only visible when I load the page.

Is there a way around it?

EDIT: I'm using jQuery v1.3.2

EDIT: Unfortunately the solution of using addClass to add css class that states display: none doesn't really work - it seemed like it did at first, but the problem is still there.

UPDATE: The js file I wrote can be found here, while the html here. The problem I have is that when you go from one portfolio to the other, the image of the first one stays there obstructing the next, even though it should be hidden (text underneath changes correctly). Wrong disappears when you try to zoom in/out of the page.
I used to hide all portfolios using $("#divId").hide(), but as was pointed out below, I now use $(".classToHide").hide().

UPDATE: The problem is solved on IE8 - I forgot to include the standards mode declaration... However, on IE6 it's still the problem.

+2  A: 

Just a thought: you're not using an old (pre-IE8) version of jQuery, are you?

Edit: No, grycz is using the current version.

RichieHindle
I'm using 1.3.2 - I think this is the latest one, right?
maciej.gryka
Yes, 1.3.2 is current. So much for that theory. 8-)
RichieHindle
A: 

Are you sure that hide() function call is even getting called on the page load? Try putting an alert('hi') right before that function call, and see if that happens in IE8.

JerSchneid
Yep - firstly because the divs disappear as soon as I zoom in/out. Secondly, because alerts show up :)
maciej.gryka
+2  A: 

Edit: simplified to use toggleClass()

You could try doing it manually, like toggling a css class called "hidden." It might look something like this:

function myToggle(element_id)
{
    mydiv = $('#' + element_id);
    mydiv.toggleClass("hidden");;
}

And your css file would have:

.hidden
{
    display:none;
}

I haven't tested this, but this is the kind of workaround I imagine you'd want to think about if this is, indeed, a bug in jQuery/IE8.

redmoskito
toggleClass('hidden'); or just toggle() guys
Chad Grant
Heh, forgot about toggleClass. Guess I've been out of the game too long. :-P Fixed in my post.
redmoskito
Correction: it seemed to work, but it still gives me the same problem... When something is `hidden` from the start it's fine, but when I assign it later, it doesn't disappear under IE.
maciej.gryka
I think the comment made elsewhere is also a problem, namely, that you're using an id to target multiple elements, which is technically illegal in the HTML standard, but works in some browsers anyway. The better way to do it is to add a class (e.g. "hide-all") to all the divs you want to hide, and then use $(".hide-all").toggleClass("hidden") to hide all of them.
redmoskito
Right - I've changed the function to hide the divs to use class instead of individual IDs - the problem stays on IE. Please see the question for the source - all the stuff is in there.
maciej.gryka
+3  A: 

You're hiding multiple div's by using an ID selector?

Try giving those div's a class "divToHide" and then use:

$(".divToHide").hide();

Maybe IE8 handles duplicate id's in another way than those other browsers..

Thomas Stock
I'm hiding multiple divs at first, but then want to show them selectively. Is it better in this case to add/remove classes from them instead of just calling show()/hide()? I doubt that would help my problem (although I'll try), but is it better for some other reason?
maciej.gryka
The advantage I see with adding/removing a css class "hidden" is that u can easily select all hidden divs by using $(".hidden") at any time.
Thomas Stock
You can also combine using IDs and classes if you want. That way u can easily hide all divs at startup using $(".divsToHide").addClass("hidden");and make divs selectively visible again using$("#widget4Div").removeClass("hidden");
Thomas Stock
A: 

try

$("#divToHide").css('display:none');