views:

254

answers:

1

I've got 3 tabs. Each of them have a div called optional, apart from other elements. optional is initially hidden using Javascript, I dun wanna use CSS ( this is so if js is disabled, the div won't be hidden at all ).

So I use this to hide optional

$(function(){ 
   $('#optional').hide();
});

Now, this works just fine on the first tab but won't hide on the next two tabs. They've all got the same code, no naming conflicts and no javascript errors reported.

Any idea what I'm doing wrong?

+2  A: 
$(function(){ 
   $('div.optional').hide();
});

Classname instead of id, since IDs are required to be unique. By the DOM/JS, if not html.


Edited: to change ...('.optional')... to ...('div.optional')... which should reduce the time it takes the function to work (since it's looking through only one set of tags <div> rather than all of them, examining them for their class-name.

David Thomas
"div.optional" is not going to be quicker than ".optional" in most browsers, since with the latter native getElementsByClassName is used...
J-P
i need to give them both unique IDs and a class name, so it doesn't matter, I'll be using IDs anyway. Thanks for your help guys... I'll wait to see if JP posts an answer since he was the first.. then accept one!
gAMBOOKa
@J-P, seriously? I always assumed that using `element.classname` meant that the browser only looked at the divs in the document, whereas `.classname` (no element) meant that the browser had to examine the class-name of *every* single element inside the body tag (possibly including the body tag, I'm not sure). I realise it'll be a near-imperceptible speed increase, but it felt worthwhile at the time.
David Thomas