Change element tag name JavaScript | Using Pure JS Example

  • by

To change the element tag name in JavaScript, simply need to make a new element and move over all the elements so you keep onclick handlers and such, and then replace the original thing.

Example of replacing specific tag name JavaScript

Complete HTML example code:-

<!DOCTYPE html>
    <style type="text/css">
        p { background-color:green;color:white; }
        div { background-color:blue;color:white; }


      function replaceTag(id){
       var that = document.getElementById(id);

       var p = document.createElement('p');

     // move all elements in the other container.
     while(that.firstChild) {


    <div id="demo">Hello WORLD

<input type="button" onclick="replaceTag('demo')" value="transform">


Code source:


replace specific tag name JavaScript

Do comment if you have any doubts and suggestions 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.