JQuery: should I check visibility before hiding an element?

Is it better to check if the element is visible before hiding it, or is it okay just to hide it? What are the benefits anyway?

In the example below, the first one checks the visibility before hiding the element .foo

...

$('.dropdown').hover(function(){
    $(this).find('.menu').fadeToggle();
    if($('.foo').is(':visible')){
        $('.foo').fadeOut();
    };
});

      

or

$('.dropdown').hover(function(){
    $(this).find('.menu').fadeToggle();
    $('.foo').fadeOut();
});

      

+3


source to share


1 answer


This is a side note from the jQuery.fadeOut () Specification :

Note. ... To avoid unnecessary DOM manipulation, .fadeOut () does not hide an element that is already considered hidden.



So relax ... There is nothing wrong with hiding an already hidden element. JQuery will check it for you.

+3


source







All Articles