|
Cookbook /
ToggleSummary: Adds links or buttons for toggling (hiding/showing) div sections and objects
Version: 2011-04-06
Prerequisites: PmWiki 2.2.0
Status: stable
Maintainer: HansB
Discussion: Toggle-Talk
Download: toggle.phpΔ
Toggle Popup BoxThis is an example of what this recipe This recipe depends on javascript.
Questions answered by this recipeHow can I add toggle switches to show and hide sections of the page? DescriptionThe script adds markup for links, buttons or images for toggling (hiding/showing) div sections and objects Download toggle.phpΔ and copy to cookbook folder. Install by adding to config.php: include_once("$FarmD/cookbook/toggle.php");
Toggle button examples: Short syntax:
Long syntax:
Toggle link examples:
Necessary parameter:
Optional parameters:
Shortcut syntax:You can avoid some of the parameter keywords and use keyless arguments in the markup instead.
LabelsFor custom labels set labels with parameters show= or hide= (or label= for one 'show' and 'hide' label). Images as togglesYou can have images displayed instead of link text. Use the image file name with extension as label text, like show=myshow.gif hide=myhide.gif. Then clicking the image will toggle the div display, and if you use different images for show and hide these images will toggle as well. Images should be attached to the page (use action=upload to upload file to page or group). Syntax like label=Attach:myimage.ext Δ does not work. If your uploaded image file is attached to a different group, for instance it is uploaded to the 'Site' group, then use syntax like show=Site/myshow.gif. - The toggle markup recognises images with extensions png, gif, jpg, jpeg and ico. If there are problems with uploading such images, make sure you follow all the advice about uploading images and necessary permissions are set. group=<classname> optionA new option to enable hiding a number of divs with class <classname> while showing the div associated with id. config.php parameters
Backward compatibilityThe script is backwards compatible with the togglelink markup syntax. lshow= works same as show= parameter, lhide= works same as hide= parameter, div= works same as id= parameter, div2= works same as id2= parameter. Buttons are implemented with a button=1 parameter, and work that way like ShowHide Buttons. This recipe supersedes the ShowHide and ToggleLink scripts. NotesThe script requires a javascript enabled modern browser. It places a javascript function into the html body, which will achieve instant toggle effects without page reloads. A toggle button can act on any named division of the page, even a sidebar or other structural division outside the normal page content. But in such a case it may not make a too good job, because to hide such a div effectively may need more than applying display:none, for instance resetting of margins. A toggle button can also act on other objects with an id= set, for instance tables. It is not restricted to divs. A toggle button or link can toggle two divisions or objects, hiding one and showing the other, alternating. Name the first division or object id with id=... and the second with id2=... Two toggle buttons or two toggle links can act together as a pair on one div. For instance put one on top of the div, and the other inside at the bottom, to conveniently close it. But note that the label or link text of the second one will be displayed on the first one when clicked, so use the same labels for both. And if the div shall be initially hidden, place init=hide into the first markup and not the second. Ensure that the skin/template you're using has a <!--HTMLFooter--> tag towards the bottom. This is needed to load the JavaScript (old version skin hasn't this tag). Tip: If you want any initially hidden div or object to be hidden while the page is loading (since some pages load slowly and javascript does not hide the divs instantaneously):
define 'hide' as a general class in pub/local.css with
and add the class 'hide' to the divs or objects, for instance like this: (:toggle abc init=hide:) >>id=abc hide<< hidden content... >><< Tip: To show a set of divs with a single click, without them all having the same id (so they can also be individually toggled) you can use the Cookbook:HttpVariables recipe. For example, you can set init=hide in config, so the toggles default to hidden. Then base the initial value of each toggle that you want to participate in "Show all" on a URL request variable, e.g
(:toggle {$?showstatus} myhiddendiv1:). Then provide on the page a link such as "[[{*$FullName}?showstatus=show|Show all]]". The page will normally hide the participating divs, but if the user clicks the link the page will reload with the selected divs all shown.Release Notes
See Also
CommentsSee discussion at Toggle-Talk User notes +10: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki. |