49

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.

user1801625
  • 1,153
  • 3
  • 13
  • 14

6 Answers6

91

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/

bdkopen
  • 494
  • 1
  • 6
  • 16
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 5
    @Guffa: `e.parentNode.replaceChild(d, e);` but +1 ;-) – I Hate Lazy Nov 15 '12 at 00:37
  • 1
    @IHateLazy @bdkopen Why is `insertBeofre()` and `removeChild()` wrong? – Shayan Jul 27 '19 at 14:11
  • 2
    I realize the original question didn't ask for this, but this doesn't take any attributes with it. In other words, if the original HTML was `bar`, it would still make the resulting HTML `
    bar
    `.
    – Loko Web Design Jun 22 '20 at 15:59
  • 1
    1 line version: `document.querySelectorAll(".yuRUbf a").forEach(e=>e.parentNode.replaceChild(Object.assign(document.createElement("div"),{innerHTML:e.innerHTML}),e));` – ninhjs.dev Jul 18 '21 at 05:26
  • Instead of `replaceChild` on the parent, I prefer to `replaceWith` directly – Cloud Mar 09 '22 at 12:57
  • @Cloud: The `replaceWIth` method is a more recent addition than `replaceChild`. Check the support information to see if it exists in the browser versions that you need to support. – Guffa Mar 11 '22 at 08:40
  • Ah makes sense, thankfully for my uses I can assume a relatively recent browser version – Cloud Mar 11 '22 at 08:51
6

Just written a jQuery plugin for this.

(function( $ ) {
  $.fn.replaceTag = function(newTag) {
    var originalElement = this[0]
    , originalTag = originalElement.tagName
    , startRX = new RegExp('^<'+originalTag, 'i')
    , endRX = new RegExp(originalTag+'>$', 'i')
    , startSubst = '<'+newTag
    , endSubst = newTag+'>'
    , newHTML = originalElement.outerHTML
    .replace(startRX, startSubst)
    .replace(endRX, endSubst);
    this.replaceWith(newHTML);
  };
})(jQuery);

Usage:

$('div#toChange').replaceTag('span')

The biggest advantage of this method is that id preserves all the attributes of the original element.

Juribiyan
  • 700
  • 8
  • 25
0

If jquery is acceptable use replaceWith.

$('span').each(function() {
    
  $(this).replaceWith($('<div>' + this.innerHTML + '</div>'));
 
});

Here is a JSFIDDLE working DEMO

Siva Manasan
  • 123
  • 4
  • 14
Gurpreet Singh
  • 20,907
  • 5
  • 44
  • 60
-2

If using jquery

 Var spantxt = $('span').text();
 $('body').append('<div>'+spantext+'</div');

Note this would only work if there was only one span, use an id selector otherwise

Jay Rizzi
  • 4,196
  • 5
  • 42
  • 71
-2

You can't do it. What you want to do is to take content of your span, then delete it and create new div and fill it with previous content.

tonino.j
  • 3,837
  • 28
  • 27
-2

Assumption: The span you want to replace is wrapped in a div with id "foo"

In pure javascript you could do something like:

 var original_html = document.getElementById('foo').innerHTML;

 original_html = original_html.replace("<span>", "<div>");

 original_html = original_html.replace(new RegExp("</span>"+$), "</div">)
 document.getElementById('foo').innerHTML=original_html;

If however you can not necessarily expect the span to be tightly wrapped by an element you can consistently get (by id or otherwise), the javascript becomes fairly complex. In either case, the real answer here is: use jQuery.

Kenzo-kun
  • 158
  • 1
  • 13
Abraham P
  • 15,029
  • 13
  • 58
  • 126
  • 3
    This approach would have all sorts of side effects. Also, using a regexp is expensive and in this case a terrible use case. – Hybrid web dev Mar 25 '21 at 00:18
  • Terrible use case how? Side effects which? You should elaborate as to not waste time when people could read something useful. – osirisgothra Jan 15 '23 at 00:13