How to change an HTML element name using jQuery ? Last Updated : 07 Jul, 2021 Comments Improve Suggest changes Like Article Like Report 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 illustrates the above approach. html <!DOCTYPE HTML> <html> <head> <title> JQuery | Change an element type. </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </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> 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. html <!DOCTYPE HTML> <html> <head> <title> JQuery | Change an element type. </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </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> Output: Before clicking on the button: After clicking on the button: Comment More infoAdvertise with us Next Article How to change the element id using jQuery ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies jQuery-Misc Similar Reads How to change the element id using jQuery ? The jQuery methods are used to change the element ID which are described below: jQuery attr() Method: This method set/return attributes and values of the selected elements. If this method is used to return the attribute value, it returns the value of first selected element. If this method is used to 3 min read How to attach a method to HTML element event using jQuery ? In this article, we are going to learn, how can we attach a method to an HTML element event using jQuery.There are various events like click(), mouseenter(), mouseout(), etc which are available in jQuery. For attaching a method to an HTML elements event, we will need a jQuery method on(), which will 2 min read How to use jQuery to Search and Replace HTML elements? Using jQuery, you can dynamically search and replace HTML elements on a webpage. By selecting elements and binding actions to events, you can manipulate their content or structure in response to user interactions or other triggers ApproachIntegrate the jQuery library hosted on a content delivery net 2 min read How to use jQuery to Search and Replace HTML elements? Using jQuery, you can dynamically search and replace HTML elements on a webpage. By selecting elements and binding actions to events, you can manipulate their content or structure in response to user interactions or other triggers ApproachIntegrate the jQuery library hosted on a content delivery net 2 min read How to replace HTML element in jQuery ? Replacing an HTML element in jQuery involves selecting an existing element on a webpage and replacing it with new content or another element. This process can be easily managed with jQuery methods like .replaceWith() or .html(), enabling dynamic and flexible content updates.Syntax replaceWith( newCo 2 min read How to Change the ID of Element using JavaScript? We are given an element and the task is to change the ID of elements using JavaScript. ID is unique for any element and it can be assigned to an element only once. JavaScript provides a method to access this id and also to manipulate the id. Syntax:Selected_element.id = newID;Below are the appraoche 2 min read Like