Author: Dana Woodman
Version: 0.1
Licence: MIT
Comments and questions can be directed at woodman.dana (at) gmail.com
The showhide plugin is an advanced version of the jQuery "toggle" function. It adds in many useful options for controlling of a target element. The primary use is to show/hide a target HTML element by clicking on a "toggle" element (usually a link).
The plugin allows you to:
The plugin is called showhide because that is what it is primarily intended for: showing/hidding of a target element. This does not mean, however, that you need to use it for showing/hidding elements. You could use it to toggle the style of a target element (not just hiding it).
Using showhide is fairly simple. The first step is to put this in the head of your page:
<script type="text/javascript" src="/path/to/jquery-latest.js"></script> <script type="text/javascript" src="/path/to/jquery.showhide.js"></script>
Next, create your HTML. Here is an example of the bar minimum you need to use showhide (see Examples for more uses):
<p>
<a href="#" title="Toggle the span" id="example-1">Toggle</a>
<span>This can be toggled.</span>
</p>
Now add in the jQuery code to call the showhide plugin:
<script type="text/javascript">
$(function() {
$('#example-1').showhide();
});
</script>
And then add in the CSS to hide the target object:
<style type="text/css">
.hide { display: none; }
</style>
... and you should be up and running.
A simple inline toggle using no options. By default showhide will toggle the next element (e.g. $(this).next()) and will default the target object to be visible.
<p>
<a href="#" title="Toggle the span" id="example-1">Toggle</a>
<span><strong>This can be toggled.</strong></span>
</p>
$('#example-1').showhide();
Use the target_obj option to set an object other than .
This can be toggled.
<p><a href="#" title="Toggle the span" id="example-2">Toggle</a></p> <p><strong>This can be toggled.</strong></p>
<script type="text/javascript">
$(function() {
$('#example-2').showhide({
target_obj: $('#example-2').parent().next();
});
});
</script>
showhide has many configuration options. The table below lists all the options available, what data type they are, their default and how to use them.
| Property | Type | Default | Description |
|---|---|---|---|
| target_obj | DOM element | null |
The DOM element to show/hide. Example of using a jQuery object: $('#toggle').showhide({
target_obj: $('#target');
});
Another example, this time using a reference to "self" (e.g. the "#toggle" DOM element): $('#toggle').showhide({
target_obj: $(this).parent().next();
});
This will select the toggle element parent's nearest sibling. Default object is the next object (e.g. |
| focus_target | DOM element | null |
A DOM input element to automatically focus on after showing (e.g. use the JavaScript $('#toggle').showhide({
focus_target: $('#id_title');
});
|
| default_open | Boolean | true |
Whether or not to show the target element by default. If If you enable cookies, the cookie value will override this. |
| show_class | String | 'show' |
The CSS class to apply to the target element to show it. |
| hide_class | String | 'hide' |
The CSS class to apply to the target element to hide it. |
| plus_class | String | 'plus' |
The CSS class to apply to the toggle element when the target is shown. Useful if you want to apply a "plus"/"minus" image to the toggle element VIA CSS. |
| plus_text | String | null |
The text to replace the toggle elements text with when shown. Useful if you want the text to change when the user clicks the toggle object (e.g. "Show form"/"Hide form"). |
| minus_class | String | 'minus' |
The CSS class to apply to the toggle element when the target is hidden. Useful if you want to apply a "plus"/"minus" image to the toggle element VIA CSS. |
| minus_text | String | null |
The text to replace the toggle elements text with when hidden. Useful if you want the text to change when the user clicks the toggle object (e.g. "Show form"/"Hide form"). |
| use_cookie | Boolean | false |
Indicates whether or not to store the user's choice in a cookie. This is useful if you want user's to be able to set their preferences of hidden/visible elements. NOTE: To use the cookie feature, you need to include the jQuery Cookie plugin on your page. |
| cookie_expires | Number | 365 |
Number of days before cookie should expire. |
| cookie_name | String | 'show_target' |
The name of the cookie to store. NOTE: This must be unique for every instance of showhide so there is no conflict of cookie values. |
Downloads are available VIA Google Code here:
http://code.google.com/p/jquery-showhide/downloads/list
showhide should be compatable with the following browsers:
If you find a browser bug or issue, please contact me at woodman.dana (at) gmail.com and let me know about it.
Please email me at woodman.dana (at) gmail.com for help.
For general jQuery help, check out the Google Group.
Written by Dana Woodman: woodman.dana (at) gmail.com