Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide element or create on the fly? [closed]

Which option is best:

  • Have an element in the markup, hidden with CSS. If JS is enabled, show that element.

  • Have no element in the markup and create it on the fly, if JS is enabled.

If the user doesn't have JS, I don't want a div to be shown. My question isn't how to code the different cases, it's which is the best and why.

EDIT: When I say "best" I mean support your answer with why it's the best for you. Can be speed/semantics/etc. What I want is to be able to have a better look at the pros and cons of each one. Also the speed is hardly an issue because it's only one element so it would be like milliseconds.

For the first option, it's probably faster, but then we have unnecessary markup and CSS rules. Because if there is no JS, there would be no point in having the element at all, much less hiding it.

Now the second option, even though it might not be as fast, I keep the different parts separate and only really load what has to be loaded.

like image 544
Jonny Sooter Avatar asked Mar 13 '13 15:03

Jonny Sooter


People also ask

How do you hide and show elements?

Style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”. document.

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.

Which property is used to hide element?

The visibility property allows the author to show or hide an element.

How do you hide an element?

You can hide an element in CSS using the CSS properties display: none or visibility: hidden . display: none removes the entire element from the page and mat affect the layout of the page. visibility: hidden hides the element while keeping the space the same.

How do I hide an element in Dom?

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>


1 Answers

DOM manipulations are the most expensive. Showing existing code is faster than injecting it.

like image 105
Diodeus - James MacFarlane Avatar answered Oct 16 '22 05:10

Diodeus - James MacFarlane