How to replace an HTML element with another one using JavaScript? Last Updated : 20 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Replacing an HTML element with another using JavaScript allows for dynamic modification of webpage content without reloading the entire page. Document Object Model (DOM) is a platform and language-neutral interface that is used by programs and scripts to dynamically access the content, style, and structure. Below are the approaches to replacing an HTML element with another one using JavaScript: Table of Content Using replaceWith()Using innerHTML PropertyUsing outerHTML PropertyMethod 1: Using replaceWith()This approach utilizes the `replaceWith()` method to replace the selected element with the specified HTML content. Example: To demonstrate replacing the HTML element with another one using the JavaScript replaceWith() method. HTML <!DOCTYPE html> <html> <head> <title>Replace Element Example</title> </head> <body> <div id="original">Original Content</div> <button onclick="replaceElement()">Replace Element</button> <script> function replaceElement() { let newElement = document.createElement('div'); newElement.textContent = 'New Content'; document.getElementById('original').replaceWith(newElement); } </script> </body> </html> Output: OutputMethod 2: Using innerHTML PropertyThis approach uses the `innerHTML` property of the parent element to replace its content with new HTML content. Example: To demonstrate replacing the HTML element with another one using the JavaScript innerHTML property. HTML <!DOCTYPE html> <html> <head> <title>Replace Element Example</title> </head> <body> <div id="parent">Original Content</div> <button onclick="replaceElement()">Replace Element</button> <script> function replaceElement() { document .getElementById('parent') .innerHTML = '<div>New Content</div>'; } </script> </body> </html> Output: OutputMethod 3: Using outerHTML PropertyThis approach replaces the entire HTML element along with its content using the `outerHTML` property. Example: To demonstrate replacing the HTML element with another one using the JavaScript outerHTML property. HTML <!DOCTYPE html> <html> <head> <title>Replace Element Example</title> </head> <body> <div id="original">Original Content</div> <button onclick="replaceElement()">Replace Element</button> <script> function replaceElement() { let newElement = '<div>New Content</div>'; document.getElementById('original').outerHTML = newElement; } </script> </body> </html> Output: Output Comment More infoAdvertise with us Next Article How to replace an HTML element with another one using JavaScript? mr_coder9933 Follow Improve Article Tags : JavaScript Web Technologies HTML HTML-Misc JavaScript-Misc HTML-Questions JavaScript-Questions +3 More Similar Reads How to remove an HTML element using JavaScript ? Removing an HTML element using JavaScript involves deleting it from the DOM, typically using methods like element.remove() or parentNode.removeChild(). This approach updates the webpage dynamically, allowing for responsive interactions by removing unwanted or outdated elements based on user actions 3 min read How to Replace the Entire HTML Node using JavaScript? To replace an entire HTML node using JavaScript, you can use methods that allow you to remove an existing node and insert a new one in its place. Approach: Using replace() method Take the new document as a form of string(eg.. Str = '< html > < /html > '). Use .replace() method on the HTM 1 min read How to add/update an attribute to an HTML element using JavaScript? To add/update an attribute to an HTML element using JavaScript, we have multiple approaches. In this article, we are going to learn how to add/update an attribute to an HTML element using JavaScript. Below are the approaches used to add/update an attribute to an HTML element using JavaScript: Table 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 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 ob 2 min read How to replace a portion of strings with another value in JavaScript ? In JavaScript, replacing a portion of strings with another value involves using the `replace()` method or regular expressions. This process is essential for text manipulation tasks like formatting, sanitization, or dynamic content generation in web development and data processing applications. We ca 3 min read Manipulating HTML Elements with JavaScript JavaScript is a powerful language that can be used to manipulate the HTML elements on a web page. By using JavaScript, we can access the HTML elements and modify their attributes, styles, and content. This allows us to create dynamic and interactive web pages. Methods to Identify the elements to man 5 min read How to remove a Class name from an Element using JavaScript ? Removing a class name from an HTML element using JavaScript is often used to dynamically modify the styling or behavior of an element based on user interactions, events, or other conditions. The operation can be achieved using classList property, that allows you to add, remove, or toggle classes. Sy 3 min read How to get/change the HTML with DOM element in JavaScript ? In order to get/access the HTML for a DOM element in JavaScript, the first step is to identify the element base on its id, name, or its tag name. Then, we can use inner.HTML or outer.HTML to get the HTML. Using the getElementById() method: This method gets/identifies the DOM elements using its ID an 3 min read How to append HTML code to a div using JavaScript ? The ability to append HTML code to a <div> Using JavaScript helps enhance user experience by enabling dynamic content updates without reloading the page. This technique is widely usedâover 85% of modern websites implement JavaScript DOM manipulation to create smoother and more interactive web 6 min read Like