OpenWetWare:Toggle

This feature allows for toggling the visibility of any part of the page.

Fun Examples
Probably the best way to learn how the toggle code works is to look at some examples (look at the source of this page).

Multiple

 * toggle item1
 * toggle item2
 * toggle both items
 * toggle item2
 * toggle both items

Show hide all
(only shows/hides items that use the Template:Hide template)

Mutual toggling

 * 1) Toggle #2
 * 2) Toggle #1

Toggling table rows/cols

 * Toggle row 1
 * Toggle row 2
 * Toggle column1
 * Toggle column2

Remote action
toggle headers/footers/navigation

Nesting
[open level 1 close level 1 ]  Level 1 [open level 2 <span class="_toggle _toggler" style="display:none;">close level 2 ] <div class="_toggle" style="display:none;"> Level 2

Tabs
<ul> <li><span class="_toggler_hide-tabs _toggler_show-tab1">tab1 </li> <li><span class="_toggler_hide-tabs _toggler_show-tab2">tab2 </li> <li><span class="_toggler_hide-tabs _toggler_show-tab3">tab3 </li> </ul>

Usage
Toggling is based on the CSS class. You can specify the class of any element with the class attribute. For wiki text, it is usually the most convenient to add a span or div and assign a class to that, e.g. wiki text here. Notice that multiple classes are separated by spaces. Some classes have special meaning and all of these special classes begin with "_toggle". For elements that should be initially hidden, set their CSS display property to none, e.g. <div class="class1" style="display:none;"...

Togglers
To define a basic toggler, add a class of the form _toggler-CLASS to an element. For example, <span class="_toggler-foo">foo toggler produces <span class="_toggler-foo">foo toggler. Note that a link is automatically created but nothing happens when the link is clicked. That's because there are no elements of class "foo" on this page. If there were any such elements, all of their visibility would be toggled (i.e. if they are currently hidden, they are shown, and vice versa).

Some different type of togglers can also be created. The more general form for toggler classes are _toggler_OPERATION-CLASS, e.g. _toggler_show-foo. The default operation is the TOGGLE operation above. Other valid operations are:
 * reset: all elements of the given class are reset to their original value (when the page was originally shown)
 * show: all elements of the given class are made visible
 * hide: all elements of the given class are hidden

Some things to note:
 * togglers can toggle other togglers or even themselves
 * togglers can have multiple toggle operations, e.g. "_toggler_hide-foo _toggler_show-bar" will hide all foo elements and show all bar elements
 * You can toggle based on element ID rather than a class. A similar syntax is used except use double hyphens after the toggler operation. For example, "_toggler--myelement" toggles only the element with ID myelement (e.g. defined as ... . Note that IDs are technically required to be unique on a page so in this form, you should expect to be toggling at most one element.

Toggle groups
To make it easier to use without having to explicitly define classes to toggle, there is a notion of a toggle group. A new toggle group is created when the class "_togglegroup" is seen in some element. This special class serves no other purpose than to delimit the boundary of a toggle group. Within a toggle group, togglers can be defined without a class to refer to all toggles within the toggle group, e.g. "_toggler" or "_toggler_reset". Toggles within a toggle group are defined using the class "_toggle".

Handling non-Javascript
If Javascript is not enabled, usually you will want all of the content (or the important parts) to be shown. There are two special classes "_toggle_initshow" and "_toggle_inithide" which set the initial value of a toggle. This has a similar effect of setting the CSS display property. However, there are a couple differences. If you set the CSS display property, then when Javascript is not enabled, the object will be permanently set with that value. For example, an object hidden with display=none can never be shown if Javascript is turned off. Alternatively, if you do not set the CSS property and instead set _toggle_inithide, then if Javascript is on, the object will be hidden (and presumably will be toggleable), but if Javascript is off, the object will shown. The main disadvantage to using Javascript to initially hide an object is that the objects may briefly appear on the browser's screen before being hidden (doesn't look too nice). However, for accessibility reasons, it is probably preferable to allow non-Javascript browsers to view all the content.

Templates
Templates can make this much easier to use. Some existing templates are:
 * Template:Hide
 * This appears to handle 99% of the cases people use where by default nothing is shown and some text is hidden. A single show or hide button before the text allows for toggling the visibility.
 * Template:Show hide all, Template:Show all, Template:Hide all
 * Template:Toggle
 * Template:Toggler

Technical Details
See MediaWiki:Common.js for the javascript code.