Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide of a hidden element...How bad?

Tags:

jquery

hide

How bad is to do a hide of a hidden element?

So, let's say the element #myelement is hidden, and I do a $('#element').hide(). Is that bad? Should I check if it's visible first?

Thanks!

like image 797
Hommer Smith Avatar asked Nov 02 '12 08:11

Hommer Smith


People also ask

How do I completely hide an element in HTML?

Completely hiding elements can be done in 3 ways: via the CSS property display , e.g. display: none; via the CSS property visibility , e.g. visibility: hidden; via the HTML5 attribute hidden , e.g. <span hidden>

How do you remove the hidden space in an element?

The following style rule hides an element on a web page: display: none; When you set the value of display to none, the affected element will disappear. This means the element will no longer take up any space on the web page.

How can you hide an element but still keep its space in document?

visibility:hidden hides the element, but it still takes up space in the layout. display:none removes the element from the document. It does not take up any space.

How do I hide a web element?

CSS Display Property. Each element has a default display value like inline-block , block , table ..etc. To hide an element with the display property, we should use display: none . When an element is hidden with display: none , all of its descendants will be removed along with it.


2 Answers

jQuery checks if the element is visible and hides only if it's visible: https://github.com/jquery/jquery/blob/master/src/css.js#L78

    } else if ( !values[ index ] && !isHidden( elem ) ) {
        jQuery._data( elem, "olddisplay", curCSS( elem, "display" ) );
    }

So you don't need to perform additional checks. Any manual check would make the performance (and readability actually) worse.

like image 85
zerkms Avatar answered Nov 07 '22 17:11

zerkms


If there is a choice between

$("my-element").hide();

and

if(!$("my-element").is(":visible")) {
    $("my-element").hide();
})

it is much simpler (better?) to just have the first line for readability sake. jQuery will do the check whether you do it or not.

like image 33
Abhilash Avatar answered Nov 07 '22 17:11

Abhilash