I want to know how can I change a tag with pure javascript like that
<span>some text</span>
I want to change it to that
<div>some text</div>
I have no idea how to do it.
getElementById('foo'). innerHTML; original_html = original_html. replace("<span>", "<div>"); original_html = original_html. replace(new RegExp("</span>"+$), "</div">) document.
To change HTML element type with JavaScript, we can use the replaceWith method. For instance, we write: const parent = document. createElement("div"); const child = document.
You can't change the tag name of an existing DOM element; instead, you have to create a replacement and then insert it where the element was.
You can't change the type of an element like that, instead you have to create a new element and move the contents into it. Example:
var e = document.getElementsByTagName('span')[0]; var d = document.createElement('div'); d.innerHTML = e.innerHTML; e.parentNode.replaceChild(d, e);
Demo: http://jsfiddle.net/Guffa/bhnWR/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With