Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Best way to hide element? ( to prevent the element from flashing before actually hiding it )

I remember that at some point Opera ( Mostlikely it was Safari instead. ) had a problem that if you used .hide() on element, it would flash briefly before it would actually hide the element.

Now if you dont want to ignore those who for some reason dont have js on in their browser you cant really use CSS to set display: none; in that element to hide it and then use js to for example fade it in.

I recently noticed that this didnt happen anymore in Opera. So, i'd like to know if this still does happen in some browsers incase ive missed that.. And asuming that this would happen. What way would be the safest way to do it? ( of course ignoring the css method in this case. )

js .hide()

js .addClass('hide') css .hide { display: none; }

Or something else?

Edit:

js element.style.display = "none"

js $(element).css({display:"none"})

Edit2: This issue might have been in Safari actually. I also got thinking that newer jquery versions might have fixed the issue.. As i think there was a few bug reports about this problem on jquery website but i couldnt find those bug reports ..Or it could still be that newer browser version fixed it.. Not sure.


Edit3:

So, I did indeed find a lot more about this when I started searching for this bug in Safari rather than Opera. Though i cant say for sure that it never happened in opera as well...

It would seem like this problem does not exist anymore and that it is safe to use .hide() but what I learned was that this $(element).css({display:"none"}) did fix the problem when the problem was still out there.

like image 221
Joonas Avatar asked Aug 21 '11 21:08

Joonas


People also ask

Which jQuery method is used to hide the elements?

jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.

How do you hide an element while reserving the space to display the 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.

Which display value is used with JavaScript to hide elements without actually deleting them?

display: none; is commonly used with JavaScript to hide and show elements without deleting and recreating them.

Which style rule should be used to hide an element but still take up empty space on the page?

visibility: hidden; makes the element invisible, but it still takes up space.


1 Answers

You should always rely on using jQuery hide() method to hide any element because it takes care of all the browsers. I haven't seen any issues in Opera though.

Even to show any element you should always rely on using show() method again for the same reason.

E.g. To show a tr element if we say tr.css("display", "block") it will not work in Firefox because it is a table row and it needs to be specified as .css("display", "table-row"). But if you use tr.show() you dont have to worry about any browser.

like image 181
ShankarSangoli Avatar answered Sep 22 '22 21:09

ShankarSangoli