Sonntag, 15. April 2012

setVisible for jQuery

I really wonder why jQuery has no setVisible(...) function?!? Here's mine:

(function($){
  $.fn.setVisible = function(visible){
    if(visible === true){
      this.show();
    }
    else{
      this.hide();
    }
  };
}(jQuery));

4 Kommentare:

  1. A handy extension for jQuery...

    The name might be a little confusing:
    hide / show manipulates CSS display property which changes rendering.

    My association of 'Visible' is more connected to the CSS 'visibility' attribute, which doesn't influence the rendering, i.e. eating the same space whether visible or not.

    Small demo: http://jsfiddle.net/USE5L/

    A must admit that I don't know a better name for your new method... :-(

    AntwortenLöschen
    Antworten
    1. I think you are right. setVisible may be a little confusing because of the CSS property visible.

      a KISS approach would be to name the function setShowHide(...)

      Löschen
  2. You might look at this:

    http://api.jquery.com/toggle/
    Especially: jQuery.toggle(showOrHide).
    This does the same as your function.

    Regards,

    AntwortenLöschen
    Antworten
    1. Thank's for the hint! This is exactly what I was looking for.

      Löschen