Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of jQuery .hide() to set visibility: hidden

In jQuery, there are .hide() and .show() methods which sets the CSS display: none setting.

Is there an equivalent function which would set the visibility: hidden setting?

I know I can use .css() but I prefer some function like .hide() or so. Thanks.

like image 608
Tomas Avatar asked Mar 08 '12 08:03

Tomas


People also ask

Is jQuery hide () is equivalent to?

jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page).

How can use visibility hidden in jQuery?

invisible = function() { return this. each(function() { $(this). css("visibility", "hidden"); }); }; $.

How do I make something invisible in jQuery?

parentNode"). click(function(){ $(". childNode"). hide(100); $(this).


2 Answers

You could make your own plugins.

jQuery.fn.visible = function() {     return this.css('visibility', 'visible'); };  jQuery.fn.invisible = function() {     return this.css('visibility', 'hidden'); };  jQuery.fn.visibilityToggle = function() {     return this.css('visibility', function(i, visibility) {         return (visibility == 'visible') ? 'hidden' : 'visible';     }); }; 

If you want to overload the original jQuery toggle(), which I don't recommend...

!(function($) {     var toggle = $.fn.toggle;     $.fn.toggle = function() {         var args = $.makeArray(arguments),             lastArg = args.pop();          if (lastArg == 'visibility') {             return this.visibilityToggle();         }          return toggle.apply(this, arguments);     }; })(jQuery); 

jsFiddle.

like image 135
alex Avatar answered Oct 08 '22 04:10

alex


There isn't one built in but you could write your own quite easily:

(function($) {     $.fn.invisible = function() {         return this.each(function() {             $(this).css("visibility", "hidden");         });     };     $.fn.visible = function() {         return this.each(function() {             $(this).css("visibility", "visible");         });     }; }(jQuery)); 

You can then call this like so:

$("#someElem").invisible(); $("#someOther").visible(); 

Here's a working example.

like image 27
James Allardice Avatar answered Oct 08 '22 06:10

James Allardice