Skip to content

Instantly share code, notes, and snippets.

@westonruter
Created May 12, 2010 22:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save westonruter/399217 to your computer and use it in GitHub Desktop.
Save westonruter/399217 to your computer and use it in GitHub Desktop.
jQuery hide() and show() supporting HTML5 hidden attribute
(function(){
// jQuery hide() and show() supporting HTML5 hidden attribute
// "All HTML elements may have the hidden content attribute set. The hidden
// attribute is a boolean attribute. When specified on an element, it indicates
// that the element is not yet, or is no longer, relevant. User agents should
// not render elements that have the hidden attribute specified."
// http://www.whatwg.org/specs/web-apps/current-work/multipage/editing.html#the-hidden-attribute
// Pointers from http://www.bennadel.com/blog/1624-Ask-Ben-Overriding-Core-jQuery-Methods.htm
var originalHide = jQuery.fn.hide;
jQuery.fn.hide = function(duration, callback){
if(!duration){
originalHide.apply(this, arguments);
this.attr('hidden', 'hidden');
}
else {
var that = this;
originalHide.apply(this, [duration, function(){
that.attr('hidden', 'hidden');
if(callback){
callback.call(that);
}
}]);
}
return this;
};
var originalShow = jQuery.fn.show;
jQuery.fn.show = function(duration, callback){
if(!duration){
originalShow.apply(this, arguments);
this.removeAttr('hidden');
}
else {
var that = this;
originalShow.apply(this, [duration, function(){
that.removeAttr('hidden');
if(callback){
callback.call(that);
}
}]);
}
return this;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment