Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery : should i check for visibility before hiding an element?

Is it best practice to check if an element is visible before hiding it, or is ok just to hide it? What are the benefits either way?

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

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

or

$('.dropdown').hover(function(){
    $(this).find('.menu').fadeToggle();
    $('.foo').fadeOut();
});
like image 892
superUntitled Avatar asked Sep 03 '25 04:09

superUntitled


1 Answers

This is a note from jQuery's .fadeOut() specs:

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

So, relax... There's nothing wrong with hiding an element that's already hidden. JQuery will check it for you.

like image 130
LcSalazar Avatar answered Sep 05 '25 00:09

LcSalazar