1
I am developing a system (for study) and I came across a situation and thought about the different possibilities of doing it. On my website, when clicking the "Create Box" button a div with images, texts, other Ivs that are inside other Ivs, summarizing, a lot of html code. I usually do this kind of thing using the createelement() method, however, with this amount of html the javascript code would become very complex and confusing, hence I had an idea of playing all the html within a javascript function and returnsby making sure that through innerHTML I could add as many boxes as I wanted just by clicking on the "Create Box" button. Here comes the question, is this an acceptable way to create dynamic html? That is, would it be good or bad practice? If it is a bad practice, as an experienced developer would do?
Example:
//Essa função está em arquivo separado
function htmlCode() {
return '<div> MUITO CÓDIGO HTML AQUI </div>';
}
...
<div id="container"></div>
<input type="button" id="addBox" />
...
<script> document.getElementById('addBox').onclick = function() {
document.getElementById('container').innerHTML += htmlCode();
} </script>
Thank you so much, that’s really what I needed.
– Edgar