What's the difference between "replace" and "update"?

Everything is in the title! Is there a real difference between those two operations?

update replaces what’s inside the element (a la setting innerHTML = ) whereas replace swaps out the element itself for something else (a la setting outerHTML = ).

Say you had HTML like this <p>Sup dawg</p>. If you did update with, say, <div>Yes yes</div>, you’d end up with <p><div>Yes yes</div></p>. But with replace you’d just have <div>Yes yes</div> — the <p> would get replaced with the <div>.

2 Likes

Ok, I get it. What’s the main benefit of this? Keeping previously associated JS behavior attached to the root? Did you have a real use case where one would be superior to the other?