JavaScript application often use one of the following methods:
- They use empty HTML elements (which exist in markup but are invisible because of
display:none) - They generate markup dynamically
- A combination of both
What is the best case for each of these methods? When should I prefer one over the other, and why?
It all depends on what you’re trying to do.
If you want to add elements to a page, and you know how many you need, you can have them on the page and hide/show them when needed.
If you want to add a dynamic number of new elements, you can just make them on-the-fly, because having them on the page to begin with may not work if you need more than you added.
You can also clone existing elements, and change their attributes, to add elements to a page.
This all depends on what you’re trying to do.
For example:
So, there are different methods for what you are trying to do.