50

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.

6
  • Are you wanting your answer in pure Javascript or are you using jQuery? Commented Nov 15, 2012 at 0:27
  • Also, do you want to replace all instances of <span> tags or just a specific one? Commented Nov 15, 2012 at 0:28
  • 1
    Take a look here, it's quite easy to do with jQuery (if you are able to use it) -> jsfiddle.net/JHmaV Commented Nov 15, 2012 at 0:28
  • 3
    I want to know with pure javascript and I want a specific one thank you! and I didnt tried anything because Ihave no clue how to do it. Commented Nov 15, 2012 at 0:30
  • 1
    It's a legitimate question. Why the down votes ? Commented Mar 27, 2014 at 12:04

6 Answers 6

94

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/

Sign up to request clarification or add additional context in comments.

7 Comments

@Guffa: e.parentNode.replaceChild(d, e); but +1 ;-)
@IHateLazy @bdkopen Why is insertBeofre() and removeChild() wrong?
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 <span class="foo">bar</span>, it would still make the resulting HTML <div>bar</div>.
1 line version: document.querySelectorAll(".yuRUbf a").forEach(e=>e.parentNode.replaceChild(Object.assign(document.createElement("div"),{innerHTML:e.innerHTML}),e));
Instead of replaceChild on the parent, I prefer to replaceWith directly
|
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.

1 Comment

He explicitly asked for a 'pure javascript' answer.
0

If jquery is acceptable use replaceWith.

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

Here is a JSFIDDLE working DEMO

2 Comments

alternatively without jquery and in a slightly different case: jsfiddle.net/CvRq2
He asked for a pure javascript answer.
-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.

2 Comments

This approach would have all sorts of side effects. Also, using a regexp is expensive and in this case a terrible use case.
Terrible use case how? Side effects which? You should elaborate as to not waste time when people could read something useful.
-3

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

1 Comment

-3

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.