benhowdle89 benhowdle89 - 5 months ago 7x
Javascript Question

.hide() or display: none? jQuery

Which am I better off doing?

is quicker than writing out
but whats the difference and what are both of them actually doing to the HTML element?


From the jQuery page about .hide():

"The matched elements will be hidden immediately, with no animation. This is roughly equivalent to calling .css('display', 'none'), except that the value of the display property is saved in jQuery's data cache so that display can later be restored to its initial value. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline."

So if it's important that you're able to revert to the previous value of display, you'd better use hide() because that way the previous state is remembered. Apart from that there's no difference.

$(function() {

function setDisplayValue() {
    var display = $('.toggle')[0].style.display;
div {
    display: table-cell;
    border: 1px solid;
    padding: 5px;
<script src=""></script>
    <button class="hide">Hide</button>
    <button class="show">Show</button>

<div class="toggle">Lorem Ipsum</div>

    The display value of the div is:
    <span class="displayvalue"></span>