Created
February 24, 2014 20:41
-
-
Save rafaelrinaldi/9196582 to your computer and use it in GitHub Desktop.
Simple jQuery plugin to toggle between shown/hidden states.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple jQuery plugin to toggle between shown/hidden states. | |
* | |
* Markup: | |
* | |
* <div class='box' style='width: 50px; height: 50px; background: red; padding: 20px;'>Box</div> | |
* <button>Toggle</button> | |
* | |
* Usage: | |
* | |
* // The box will toggle its visibility every time user clicks on `button`. | |
* $('button').click(function() { | |
* $('.box').visibilityToggle(); | |
* }); | |
*/ | |
(function($) { | |
$.fn.visibilityToggle = function() { | |
var context = $(this), | |
isHidden = context.css('display') === 'none'; | |
if(isHidden) { | |
context.show(); | |
} else { | |
context.hide(); | |
} | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Turns out that using the native
toggle()
method without any parameters will toggle visibility.