TMS TMS - 1 year ago 163
jQuery Question

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

In jQuery, there are

methods which sets the CSS
display: none

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

I know I can use
but I prefer some function like
or so. Thanks.

Answer Source

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);