The $.html()
has two functions:
1) Return the internal HTML of an element:
<div>
<p>Parágrafo</p> <---
<a href="url">Link</a> | HTML interno
<br> <---
</div>
In doing $("div").html()
will return the HTML code inside the div.
2) Add HTML to an element by replacing what is:
In doing $("div").html("Olá mundo!")
will replace the internal div HTML, resulting in:
<div>
Olá mundo!
</div>
The $.append()
only adds HTML at the end of the element (it does not replace what there is).
In doing $("div").append("Olá mundo!")
, the result will be:
<div>
<p>Parágrafo</p>
<a href="url">Link</a>
<br>
Olá mundo!
</div>
When you use one or the other will depend on what you want to do. If you just want to add HTML to an element keeping what already exists, use the .append()
. If you want to replace the widget’s HTML with another, use .html()
.
I think with jquery the DOM is always overwritten, the difference is that with the
append()
it overcoats keeping the previous one and adding a new one, and with thehtml()
it overwrites, excluding the previous– Vinicius De Jesus
Yes, that’s what my words meant.
append()
included andhtml()
replace– Vinicius Dutra
pq in case the DOM is always rewritten, unless it is using a framework like the React that uses the VDOM that prevents the full reload of the gift and only overwrites object that should be overwritten, improving the performance of the page.
– Vinicius De Jesus