tags:

views:

113

answers:

4

If I have an element on a web page that I expect to be showing and hiding quite a bit using javascript, which would be the most appropriate way to set it as initially invisible?

<div class="hidden">...</div>

or

<div style="display: none;">...</div>

Using class="..." is preferable to style="..." in most cases, but I'm not convinced it's the best fit in this case. Semantically, my element isn't a hidden element, it's just one that will start off hidden when the page first loads. As I'm using jQuery's show() and hide() methods, it means it'll often be in this state:

<div class="hidden" style="display: block;">...</div>

.. which to me is plainly nonsense.

On the other hand, using an in-line style="display: none;" feels somehow hacky and hard-coded.

I'm aware that either method will work perfectly and the user will never be any the wiser, but which pattern violates design principles the least?

+5  A: 

Call the style initially-hidden. It accurately conveys the information, and makes sense if you were to ever query for that style using jQuery.

John Calsbeek
This is beautiful.
teedyay
+4  A: 

I think if you're going to toggle by changing display around from "" to "none", you should use style. If you're going to toggle by changing class from "hidden" to "", you should use class. In other words, be consistent.

Brian Schroth
If you're planning on using a JavaScript framework to do this, you should probably use the convention from it. For example, Prototype uses the style="display: none;" method, then you call Element.show() and Element.hide() later to change it.
Jason
A: 

Use jQuery, and its easy-peasy:

http://docs.jquery.com/Effects/toggle

Very cool, and easy to use!

tahdhaze09
A: 

While I agree about having a whole class to just call out the initial visibility of something might seem a bit much it does make it easier to figure out what is going on. Especially if you name it something descriptive such as 'initially-hidden' as was suggested earlier.

So while I can't call out a specific design principal I think what I'm suggesting has its merits. This is more maintainable than either applying a direct style that you could not reuse and more obvious as to whats happening than a call to hide() using jQuery.

Robb C
He's using the class as a target for the JS. It's needed. You don't even need to include in the stylesheet. In fact, since the class is for the JS/jQuery only, you shouldn't have it the stylesheet at all. var flip = 0; $("button").click(function () { $("div").toggle( flip++ % 2 == 0 ); });display:none is fine and works accurately for the toggle. Click the button, it appears, click again and it disappers. Exactly the way askers wants it to work.Flips the div on and off.
tahdhaze09
I agree it works as the asker wants but the asker did present different solutions. The questions wasn't 'how should I do this' it was 'what is better'.
Robb C
I'm using the class to define the visibility of the element when the page first loads. In the css, .hidden (or .initially-hidden) would be { display: none; }. I wouldn't use the class as my jQuery selector - jQuery would probably select the individual div by id, or some other rule.
teedyay