Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery css display property set to ''" is it valid usage?

I have used the following jquery code to hide and show an element on a click event of a button

<div id='testDiv'>Test</div>
$('#testDiv').css('display','none');
$('#testDiv').css('display','');

There are plenty of examples on setting the display to block,inline etc and also use of jquery hide and show. The above code i used on my page works fine but i do not know if its correct use of the display. Can anyone please let me know if i should stop using it this way and use a valid property like 'block'. I used this as there was no initial display property set for the 'testDiv'.

like image 306
hitech0101 Avatar asked Oct 13 '16 16:10

hitech0101


People also ask

What does the CSS display property allow you to do?

The display CSS property sets whether an element is treated as a block or inline element and the layout used for its children, such as flow layout, grid or flex. Formally, the display property sets an element's inner and outer display types.

How check display is blocked or not in jQuery?

Answer: Use the jQuery :visible Selector You can use the jQuery :visible selector to check whether an element is visible in the layout or not. This selector will also select the elements with visibility: hidden; or opacity: 0; , because they preserve space in the layout even they are not visible to the eye.

Which values of the display property allow the next element to sit beside it?

An inline element will accept margin and padding, but the element still sits inline as you might expect. Margin and padding will only push other elements horizontally away, not vertically.


1 Answers

It is documented by the jquery documentation as a valid way of removing a style that you have previously manipulated with jquery. It will set it back to the previous style. Be aware of some caveats though:

Setting the value of a style property to an empty string — e.g. $( "#mydiv" ).css( "color", "" ) — removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css() method, or through direct DOM manipulation of the style property. As a consequence, the element's style for that property will be restored to whatever value was applied. So, this method can be used to cancel any style modification you have previously performed. It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or element. Warning: one notable exception is that, for IE 8 and below, removing a shorthand property such as border or background will remove that style entirely from the element, regardless of what is set in a stylesheet or element.

From the jquery docs here:

http://api.jquery.com/css/

like image 138
Eric G Avatar answered Oct 02 '22 23:10

Eric G