How to change an HTML element name using jQuery ?
Given an HTML document and the task is to replace an HTML element by another element. For example: we can change an element <b> with <h1> element without changing any other property.
Approach:
- Select the HTML element which need to be change.
- Copy all attributes of previous element in an object.
- Replace the previous element by new element.
Example 1: This example illutrates the above approach.
<!DOCTYPE HTML> <html> <head> <title> JQuery | Change an element type. </title> <script src= </script> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksforGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <br><br> <b style = "color:green; font-weight: bold;"> Welcome to GeeksforGeeks </b> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = "Click on the button to " + "replace an HTML element"; function GFG_Fun() { var attribute = { }; $.each($("b")[0].attributes, function(id, atr) { attribute[atr.nodeName] = atr.nodeValue; }); $("b").replaceWith(function () { return $("<h1 />", attribute).append($(this).contents()); }); } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before clicking on the button:

- After clicking on the button:

Example 2: This example using the above discussed approach but with a different way to copy attributes.
<!DOCTYPE HTML> <html> <head> <title> JQuery | Change an element type. </title> <script src= </script> </head> <body id = "body" style = "text-align:center;"> <h1 style = "color:green;" > GeeksforGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <br><br> <b style = "color:green; font-weight: bold;"> Welcome to GeeksforGeeks </b> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = "Click on the button to " + "replace an HTML element"; function GFG_Fun() { var oldElement = $("b"); var newElement = $("<h1>"); for(var i=0; i<oldElement[0].attributes.length; i++) { var Attr = oldElement[0].attributes[i].nodeName; var AttrVal = oldElement[0].attributes[i].nodeValue; newElement.attr(Attr, AttrVal); } newElement.html(oldElement.html()); oldElement.replaceWith(newElement); } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before clicking on the button:

- After clicking on the button:

Recommended Posts:
- How to change the element id using jQuery ?
- JQuery | Change the text of a span element
- How to change the src attribute of an img element in JavaScript / jQuery ?
- How to select html element using ID in jQuery when ID contains a dot character ?
- How to check an HTML element is empty using jQuery ?
- jQuery | change() with Examples
- How to change the checkbox value using jQuery ?
- How to change cursor style using jQuery ?
- How to change the background image using jQuery ?
- How to Change the Text of a Button using jQuery?
- How to change the placeholder text using jQuery?
- Content editable change event in jQuery
- How to detect and change the content/style of a div using jQuery ?
- How to change selected value of a drop-down list using jQuery?
- How to detect value change on hidden input field in JQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

