Is it better to create hidden elements and then show them on click or create and add to the DOM by clicking with jQuery

Is it better to create hidden elements and then show them on a click (for example, on an event) or create and add to the DOM by clicking with jQuery? In this case, the performance will be better?

$('<div/>', { 'id':'createdafterhtmlloaded', 'style':' ', 'html':'' }).appendTo('.cont'); 
+6
source share
2 answers

I always prefer to optimize the initial load. Depending on the size, of course, as soon as I start getting more than 4 or 5 hidden elements, I start thinking about loading the page.

Here is a nice article that contains hidden elements and a few other jquery optimization tips: javascript

Good luck

0
source

Dynamically creating new DOM elements will be a small tiny bit, less efficient than switching the properties of elements that already exist in the document.

But such optimization is really only a consideration in very large, terribly complex single-page applications made of thousands and thousands of dynamic elements.

For everyday web development, your decision should be based primarily on what suits you best, a modest, redesigned, underpaid web developer.

0
source

All Articles