When developing html interface elements, this is a fairly common script that shows / hides the DOM depending on certain events. My question is simple, but every time I come across this, I always ask myself: “It is better” to have elements that are hardcoded in html and just switch the display attribute (for example, with .show / .hide) or dynamically add / remove them as needed through js? Both are trivial to implement, but I can't help but wonder how they compare and if there is an advantage / disadvantage to using one over the other.
There are cases, for example, when the same element is used literally in several places, where it seems to make sense to create dynamically dynamically as you go, but, on the other hand, hard coding them is theoretically more convenient, since you you can move the DOM around and change them as needed, and they will work as expected, as long as the selector is still the same for jQuery. Generally speaking, from a design approach it seems that hard coding is the way to go, but I look forward to thoughts and, possibly, things that I can ignore here.
Edit: "hardcoded" I meant elements that were not inserted through JS; that is, elements that have their own position, already indicated in their parent document, in the original html markup, and not in JS.
source
share