Change tag using JavaScript | Span to div example code

  • by

How can I change a tag with pure JavaScript like that?

<span>some text</span>

change span tag to div tag etc.

<div>some text</div>

Example how to Change tag using JavaScript

Use innerHTML and replace the method with a regular expression to change the tag name JS.

<!DOCTYPE html>
    <span class="span1"> Text in span</span>
    <script type="text/javascript">
        var elem = document.getElementsByTagName('body')[0];
        var target = elem.innerHTML;
        elem.innerHTML = target.replace(/(<span)/igm, '<div').replace(/<\/span>/igm, '</div>');


Change tag using JavaScript

Do comment if you have another example or way to do it. You can also do comment if have any suggestions or doubts on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.