8
When I need to create elements in the DOM through jQuery, I usually use the following syntax:
$('<div>').addClass('minha-div').attr({id: 'id-da-div'});
However, what I usually see in Internet tutorials is a little different. Is usually:
$('<div></div>').addClass('minha-div').attr({id: 'id-da-div'});
Or:
$('<div id="id-da-div" class="minha-div"></div>');
I wonder if, because I didn’t close the div in the first example, there could be some problem (until today it worked correctly) and if this is bad practice.
I must create the attributes by the functions of jQuery (as in the first and second example) or I must "write it" directly into the argument where I pass the selector (third example)?
Thank you! you took away all my doubts.
– Wallace Maxters
For the second question, personally I find it more elegant and practical through a JSON (first two examples) than a giant, unidentifiable and possibly difficult to interpret string.
– Bruno Augusto