Change tag using javascript [duplicate]

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/

Leave a Comment