Skip to main content

How do you test if something is hidden with jQuery?


In jQuery, suppose you have an element of some kind that you're hiding and showing, using .hide() , .show() or .toggle() . How do you test to see if that element is currently hidden or visible on the screen?



Source: Tips4allCCNA FINAL EXAM

Comments

  1. As, the question refers to a single element, this code might be more suitable:

    $(element).is(":visible") // Checks for display:[none|block], ignores visible:[true|false]


    Same as twernt's suggestion, but applied to a single element.

    ReplyDelete
  2. You can use the "hidden" and "visible" selectors.

    $('element:hidden')


    http://docs.jquery.com/Selectors/hidden

    $('element:visible')


    http://docs.jquery.com/Selectors/visible

    ReplyDelete
  3. $(element).css('display') == 'none'


    Functions don't work with visibility attribute.

    ReplyDelete
  4. Tsvetomir's solution worked for me, couldn't post a comment though. Expanding on it...

    if( $(element).is(":visible") ) {
    // element is visible
    }
    else {
    // element is not visible
    }

    ReplyDelete
  5. None of these answers address what I understand to be the question, which is what I was searching for, "how do I handle items that have visibility == hidden?". Neither :visible nor :hidden will handle this, as they are both looking for display per the documentation. As far as I could determine, there is no selector to handle CSS visibility. Here is how I resolved it (standard jQuery selectors, there may be a more condensed syntax):

    $(".item").each(function()
    {
    if ($(this).css("visibility") == "hidden")
    {
    // handle non visible state
    }
    else
    {
    // handle visible state
    }
    })

    ReplyDelete
  6. It's worth mentioning (even after all this time), that $(element).is(":visible") works for jQuery 1.4.4, but not for jQuery 1.3.2, under IE8.

    This can be tested using Tsvetomirs helpful test snippet. Just remember to change the version of jQuery, to test under each one.

    ReplyDelete
  7. If you already have a reference to a particular element and you want to perform some action on it only if it is visible, or only if it is hidden then you can do do the following. This basically allows you to do the following, but without the 'if' statement :

    if ($(button).is(":visible")) {
    $(button).animate({ width: "toggle" }); // hide button
    }


    Here's how to do it without the 'if' :

    var button = $('#btnUpdate')[0];

    if (weWantToHideTheButton)
    {
    // hide button by sliding to left
    $(button).filter(":visible").animate({ width: "toggle" });
    }
    else {
    // show button by sliding to right
    $(button).filter(":hidden").animate({ width: "toggle" });
    }


    This uses the same :visible or :hidden check, but acts on a specific element we already have previously selected (the variable button).

    In this case I wanted to do this, but in only one line.

    ReplyDelete
  8. This works for me, and I am using show() and hide() to make my div hidden/visible

    if( $(this).css("display") == 'none' ){

    /* your code here*/
    }
    else{

    /* alternate logic */
    }

    ReplyDelete
  9. I use css class .hide { display: none!important; }, for hiding/showing I call .addClass("hide")/.removeClass("hide"), for checking visibility i use .hasClass("hide").
    It's simple and clear way to check/hide/show elements, if you don't plan to use .toggle() or .animate() methods.

    ReplyDelete
  10. The :visible selector according to jquery documentation:

    They have a CSS display value of none.
    They are form elements with type="hidden".
    Their width and height are explicitly set to 0.
    An ancestor element is hidden, so the element is not shown on the page.
    Elements with visibility: hidden or opacity: 0 are considered to be visible, since they still consume space in the layout.

    This is useful in some cases and useless in others, because if you want to check if the element is visible display != none ignoring the parents visibility you will find that doing this .css("display") == 'none' is not only faster but also will return the visibility check correctly.

    If you want to check visibility instead of display you should use: .css("visibility") == "hidden".

    Also take in consideration the additional jquery notes:

    Because :visible is a jQuery extension and not part of the CSS specification, queries using :visible cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :visible to select elements, first select the elements using a pure CSS selector, then use .filter(":visible").

    Also if you are concern about performance you should check this link:

    http://www.learningjquery.com/2010/05/now-you-see-me-showhide-performance

    And use other methods to show and hide elements.

    ReplyDelete
  11. Element could be hidden with "display:none", "visibility:hidden" or "opacity:0". Difference between those methods:


    display:none hides the element and it does not take up any space;
    visibility:hidden hides the element, but it still takes up space in the layout;
    opacity:0 hides the element as "visibility:hidden" and it still takes up space in the layout; the only difference is that opacity let to do element partly transparent;


    How element visibility and jQuery works;

    if ($('.target').is(':hidden')) {
    $('.target').show();
    } else {
    $('.target').hide();
    }

    if ($('.target').is(':visible')) {
    $('.target').hide();
    } else {
    $('.target').show();
    }

    if ($('.target-visibility').css('visibility') == 'hidden'){
    $('.target-visibility').css({visibility: "visible", display: ""});
    }else{
    $('.target-visibility').css({visibility: "hidden", display: ""});
    }

    if ($('.target-visibility').css('opacity') == "0"){
    $('.target-visibility').css({opacity: "1", display: ""});
    }else{
    $('.target-visibility').css({opacity: "0", display: ""});
    }


    Useful jQuery toggle methods:

    $('.click').click(function() {
    $('.target').toggle();
    });

    $('.click').click(function() {
    $('.target').slideToggle();
    });

    $('.click').click(function() {
    $('.target').fadeToggle();
    });

    ReplyDelete
  12. Another answer you should put into consideration is if you are hiding an element, you should use jquery, but instead of actually hiding it, you remove the whole element but you copy its html content and the tag itself into a jquery variable, and then all you need to do is test if there is such a tag on the screen, using the normal if ($('#thetagname')).

    ReplyDelete
  13. One Can simply use hidden or visible attribute like

    $('element:hidden')
    $('element:visible')


    or You can simplfy the same with IS as following:-

    $(element).is(":visible")

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex