I am looking to replace an element in the DOM.
For example, there is an <a>
element that I want to replace with a <span>
instead.
How would I go and do that?
I am looking to replace an element in the DOM.
For example, there is an <a>
element that I want to replace with a <span>
instead.
How would I go and do that?
a is the replaced A element.
This question is very old, but I found myself studying for a Microsoft Certification, and in the study book it was suggested to use:
I looked it up and it seems to only be supported in IE. Doh..
I thought I'd just add it here as a funny side note ;)
A.replaceWith(span)
- No parent neededGeneric form:
Way better/cleaner than the previous method.
For your use case:
Mozilla Docs
Supported Browsers - 86% Nov 2018
by using replaceChild():
Example for replacing LI elements