Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does JQuery do to a <div> when you set it to hide?

Tags:

jquery

dom

I was wondering what JQuery actually does to a <div> when you set it to hide?

Does it simply set it's CSS display style to none?

The reason i ask, i wanted to set a <div> to be hidden initially (by setting the property manually, not by calling .hide) then use .show() to bring it into view when needed.

like image 543
4imble Avatar asked Jun 03 '10 10:06

4imble


2 Answers

It sets it to display: none, though some extra work comes on, that's the overall effect.

If you have a <div style="display: none;"> then .show() will show it :)

Here's a quick demo showing this

If you're more curious as to exactly what happens, you can always look directly at the source :) The .hide() function inner-workings can be seen here: http://github.com/jquery/jquery/blob/master/src/effects.js#L59

The main difference between .hide()/.show() and .css('display':'whatever') for example, is that .show() restores the previous display value (or clears it so it's default if it was never hidden with .hide()), instead of just picking a new one :)

like image 63
Nick Craver Avatar answered Oct 07 '22 15:10

Nick Craver


According to the JQuery API with speed set to none, yes

Otherwise when it will do the following (also from the API documentation):

When a duration is provided, .hide() becomes an animation method. 
The .hide() method animates the width, height, and opacity of the 
matched elements simultaneously. When these properties reach 0,
the display style property is set to none to ensure that the element
no longer affects the layout of the page.
like image 40
Dan McGrath Avatar answered Oct 07 '22 14:10

Dan McGrath