{"id":92,"date":"2022-07-01T10:00:00","date_gmt":"2022-07-01T10:00:00","guid":{"rendered":"https:\/\/javascriptguide.com\/?p=92"},"modified":"2022-07-26T08:47:02","modified_gmt":"2022-07-26T08:47:02","slug":"node-replacechild","status":"publish","type":"post","link":"https:\/\/javascriptguide.com\/node-replacechild\/","title":{"rendered":"Node.replaceChild()"},"content":{"rendered":"<div class='flex_column av-nl2g-5908c493be094eaf73acb3adbedb32cd av_one_full  avia-builder-el-0  avia-builder-el-no-sibling  first flex_column_div '     ><section  class='av_textblock_section av-l51dnfcw-71f68ae24e348e20ac022caf5efcc756'  itemscope=\"itemscope\" itemtype=\"https:\/\/schema.org\/BlogPosting\" itemprop=\"blogPost\" ><div class='avia_textblock'  itemprop=\"text\" ><p>JavaScript\u2019s <code class=\"language-javascript\">Node.replaceChild()<\/code> method replaces a child node within the <code class=\"language-javascript\">Node<\/code>.<\/p>\n<h2>Syntax<\/h2>\n<pre><code class=\"language-javascript\">Node.replaceChild(newChild, oldChild);<\/code><\/pre>\n<h3>Parameters<\/h3>\n<p><code class=\"language-javascript\">newChild<\/code><br \/>\nThe <code class=\"language-javascript\">Node<\/code> object to replace <code class=\"language-javascript\">oldChild<\/code>.<\/p>\n<p>If <code class=\"language-javascript\">newChild<\/code> is a reference to an existing node in the document, the method moves the node from its current position to the new position.<\/p>\n<p><code class=\"language-javascript\">oldChild<\/code><br \/>\nThe <code class=\"language-javascript\">Node<\/code> object to be replaced.<\/p>\n<h3>Return value<\/h3>\n<p>The replaced node\u00a0(<code class=\"language-javascript\">oldChild<\/code>).<\/p>\n<h3>Exceptions<\/h3>\n<p><code class=\"language-javascript\">HierarchyRequestError<\/code> <code class=\"language-javascript\">DOMException<\/code><\/p>\n<p>Thrown when the constraints of the DOM are violated (for example, when the node that replaces another node is an ancestor of the parent node, which leads to a cycle).<\/p>\n<p><code class=\"language-javascript\">NotFoundError DOMException<\/code><\/p>\n<p>Thrown if the parent of <code class=\"language-javascript\">oldChild<\/code> is not the current node.<code class=\"language-javascript\"><\/code><\/p>\n<h2>Examples<\/h2>\n<p>Here are some examples of using the JavaScript <code class=\"language-javascript\">replaceChild()<\/code> method.<\/p>\n<p>Suppose we have the following lists of programming languages:<\/p>\n<pre><code class=\"language-xml\">&lt;ul id=\"list\"&gt;\n\u2003&lt;li&gt;JavaScript&lt;\/li&gt;\n\u2003&lt;li&gt;Python&lt;\/li&gt;\n&lt;\/ul&gt;\n\n&lt;ul id=\"newList\"&gt;\n\u2003&lt;li&gt;C#&lt;\/li&gt;\n&lt;\/ul&gt;<\/code><\/pre>\n<p>We would like to replace C# with JavaScript. How could we do that?<\/p>\n<p>One way to do that is to select the second list with the <a href=\"https:\/\/javascriptguide.com\/document-getelementbyid\/\"><code class=\"language-javascript\">getElementById()<\/code><\/a> method, select the first <code class=\"language-xml\">li<\/code> elements of both lists, and use the <code class=\"language-javascript\">replaceWith()<\/code> method:<\/p>\n<pre><code class=\"language-javascript\">let newList = document.getElementById('newList');\nlet newChild = document.querySelector('#list li:first-child');\nlet oldChild = document.querySelector('#newList li:first-child');\nnewList.replaceChild(newChild, oldChild);<\/code><\/pre>\n<p>Now our HTML looks like this:<\/p>\n<pre><code class=\"language-xml\">\n&lt;ul id=\"list\"&gt;\n\u2003&lt;li&gt;Python&lt;\/li&gt;\n&lt;\/ul&gt;\n\n&lt;ul id=\"newList\"&gt;\n\u2003&lt;li&gt;JavaScript&lt;\/li&gt;\n&lt;\/ul&gt;<\/code><\/pre>\n<p>If we only had the second list, we could replace C# with JavaScript by creating a new <code class=\"language-xml\">li<\/code> element:<\/p>\n<pre><code class=\"language-javascript\">let newList = document.getElementById('newList');\nlet newChild = document.createElement('li');\nnewChild.textContent = 'JavaScript';\nlet oldChild = document.querySelector('#newList li:first-child');\nnewList.replaceChild(newChild, oldChild);<\/code><\/pre>\n<h2>Browser compatibility<\/h2>\n<div class=\"table-responsive\"><table>\n    <thead>\n      <tr>\n        <td><\/td>\n        <th class=\"platform\" colspan=\"6\"><i class=\"fa-solid fa-desktop\"><\/i><\/th>\n        <th class=\"platform\" colspan=\"6\"><i class=\"fa-solid fa-mobile-alt\"><\/i><\/th>\n      <\/tr>\n      <tr>\n        <td><\/td>\n        <th>\n          <div class=\"browser\">Chrome<\/div>\n          <div><\/div>\n        <\/th>\n        <th>\n          <div class=\"browser\">Edge<\/div>\n          <div><\/div>\n        <\/th>\n        <th>\n          <div class=\"browser\">Firefox<\/div>\n          <div><\/div>\n        <\/th>\n        <th>\n          <div class=\"browser\">Internet Explorer<\/div>\n          <div><\/div>\n        <\/th>\n        <th>\n          <div class=\"browser\">Opera<\/div>\n          <div><\/div>\n        <\/th>\n        <th>\n          <div class=\"browser\">Safari<\/div>\n          <div><\/div>\n        <\/th>\n        <th>\n          <div class=\"browser\">Chrome (Android)<\/div>\n          <div><\/div>\n        <\/th>\n        <th>\n          <div class=\"browser\">Firefox (Android)<\/div>\n          <div><\/div>\n        <\/th>\n        <th>\n          <div class=\"browser\">Opera (Android)<\/div>\n          <div><\/div>\n        <\/th>\n        <th>\n          <div class=\"browser\">Safari (iOS)<\/div>\n          <div><\/div>\n        <\/th>\n        <th>\n          <div class=\"browser\">Samsung Internet<\/div>\n          <div><\/div>\n        <\/th>\n        <th>\n          <div class=\"browser\">WebView (Android)<\/div>\n          <div><\/div>\n        <\/th>\n      <\/td>\n    <\/thead>\n    <tbody>\n      <tr>\n        <th><code class=\"language-javascript\">replaceChild()<\/code><\/th>\n        <td class=\"version\"><i class=\"fa-solid fa-check\"><\/i>1<\/td>\n        <td class=\"version\"><i class=\"fa-solid fa-check\"><\/i>12<\/td>\n        <td class=\"version\"><i class=\"fa-solid fa-check\"><\/i>1<\/td>\n        <td class=\"version\"><i class=\"fa-solid fa-check\"><\/i>6<\/td>\n        <td class=\"version\"><i class=\"fa-solid fa-check\"><\/i>7<\/td>\n        <td class=\"version\"><i class=\"fa-solid fa-check\"><\/i>1.1<\/td>\n        <td class=\"version\"><i class=\"fa-solid fa-check\"><\/i>18<\/td>\n        <td class=\"version\"><i class=\"fa-solid fa-check\"><\/i>4<\/td>\n        <td class=\"version\"><i class=\"fa-solid fa-check\"><\/i>10.1<\/td>\n        <td class=\"version\"><i class=\"fa-solid fa-check\"><\/i>1<\/td>\n        <td class=\"version\"><i class=\"fa-solid fa-check\"><\/i>1.0<\/td>\n        <td class=\"version\"><i class=\"fa-solid fa-check\"><\/i>4.4<\/td>\n      <\/tr>\n    <\/tbody>\n  <\/table><\/div>\n<h2>See also<\/h2>\n<ul>\n<li><a href=\"https:\/\/javascriptguide.com\/node-appendchild\/\">Node.appendChild()<\/a><\/li>\n<li><a href=\"https:\/\/javascriptguide.com\/node-haschildnodes\/\">Node.hasChildNodes()<\/a><\/li>\n<li><a href=\"https:\/\/javascriptguide.com\/node-removechild\/\">Node.removeChild()<\/a><\/li>\n<\/ul>\n<\/div><\/section><\/div>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript\u2019s Node.replaceChild() method replaces a child node within the Node.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5],"class_list":["post-92","post","type-post","status-publish","format-standard","hentry","category-methods","tag-code"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript replaceChild() - JavaScript Guide<\/title>\n<meta name=\"description\" content=\"JavaScript\u2019s Node.replaceChild() method replaces a child node within the Node.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/javascriptguide.com\/node-replacechild\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript replaceChild() - JavaScript Guide\" \/>\n<meta property=\"og:description\" content=\"JavaScript\u2019s Node.replaceChild() method replaces a child node within the Node.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/javascriptguide.com\/node-replacechild\/\" \/>\n<meta property=\"og:site_name\" content=\"JavaScript Guide\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-01T10:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-26T08:47:02+00:00\" \/>\n<meta name=\"author\" content=\"Ville\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/javascriptguide.com\/#website\",\"url\":\"https:\/\/javascriptguide.com\/\",\"name\":\"JavaScript Guide\",\"description\":\"Learn JavaScript programming\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/javascriptguide.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/javascriptguide.com\/node-replacechild\/\",\"url\":\"https:\/\/javascriptguide.com\/node-replacechild\/\",\"name\":\"JavaScript replaceChild() - JavaScript Guide\",\"isPartOf\":{\"@id\":\"https:\/\/javascriptguide.com\/#website\"},\"datePublished\":\"2022-07-01T10:00:00+00:00\",\"dateModified\":\"2022-07-26T08:47:02+00:00\",\"author\":{\"@id\":\"https:\/\/javascriptguide.com\/#\/schema\/person\/3e038f42465663dfcd1871ee3da7b3ba\"},\"description\":\"JavaScript\u2019s Node.replaceChild() method replaces a child node within the Node.\",\"breadcrumb\":{\"@id\":\"https:\/\/javascriptguide.com\/node-replacechild\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/javascriptguide.com\/node-replacechild\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/javascriptguide.com\/node-replacechild\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/javascriptguide.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Node.replaceChild()\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/javascriptguide.com\/#\/schema\/person\/3e038f42465663dfcd1871ee3da7b3ba\",\"name\":\"Ville\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/javascriptguide.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/637736025a2aea7450783c43bbb72c85510b2b90835e19a101e81dbbf23e58f3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/637736025a2aea7450783c43bbb72c85510b2b90835e19a101e81dbbf23e58f3?s=96&d=mm&r=g\",\"caption\":\"Ville\"},\"sameAs\":[\"https:\/\/javascriptguide.com\"],\"url\":\"https:\/\/javascriptguide.com\/author\/chief\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript replaceChild() - JavaScript Guide","description":"JavaScript\u2019s Node.replaceChild() method replaces a child node within the Node.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/javascriptguide.com\/node-replacechild\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript replaceChild() - JavaScript Guide","og_description":"JavaScript\u2019s Node.replaceChild() method replaces a child node within the Node.","og_url":"https:\/\/javascriptguide.com\/node-replacechild\/","og_site_name":"JavaScript Guide","article_published_time":"2022-07-01T10:00:00+00:00","article_modified_time":"2022-07-26T08:47:02+00:00","author":"Ville","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/javascriptguide.com\/#website","url":"https:\/\/javascriptguide.com\/","name":"JavaScript Guide","description":"Learn JavaScript programming","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/javascriptguide.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/javascriptguide.com\/node-replacechild\/","url":"https:\/\/javascriptguide.com\/node-replacechild\/","name":"JavaScript replaceChild() - JavaScript Guide","isPartOf":{"@id":"https:\/\/javascriptguide.com\/#website"},"datePublished":"2022-07-01T10:00:00+00:00","dateModified":"2022-07-26T08:47:02+00:00","author":{"@id":"https:\/\/javascriptguide.com\/#\/schema\/person\/3e038f42465663dfcd1871ee3da7b3ba"},"description":"JavaScript\u2019s Node.replaceChild() method replaces a child node within the Node.","breadcrumb":{"@id":"https:\/\/javascriptguide.com\/node-replacechild\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/javascriptguide.com\/node-replacechild\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/javascriptguide.com\/node-replacechild\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/javascriptguide.com\/"},{"@type":"ListItem","position":2,"name":"Node.replaceChild()"}]},{"@type":"Person","@id":"https:\/\/javascriptguide.com\/#\/schema\/person\/3e038f42465663dfcd1871ee3da7b3ba","name":"Ville","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/javascriptguide.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/637736025a2aea7450783c43bbb72c85510b2b90835e19a101e81dbbf23e58f3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/637736025a2aea7450783c43bbb72c85510b2b90835e19a101e81dbbf23e58f3?s=96&d=mm&r=g","caption":"Ville"},"sameAs":["https:\/\/javascriptguide.com"],"url":"https:\/\/javascriptguide.com\/author\/chief\/"}]}},"_links":{"self":[{"href":"https:\/\/javascriptguide.com\/wp-json\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javascriptguide.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javascriptguide.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javascriptguide.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javascriptguide.com\/wp-json\/wp\/v2\/comments?post=92"}],"version-history":[{"count":15,"href":"https:\/\/javascriptguide.com\/wp-json\/wp\/v2\/posts\/92\/revisions"}],"predecessor-version":[{"id":382,"href":"https:\/\/javascriptguide.com\/wp-json\/wp\/v2\/posts\/92\/revisions\/382"}],"wp:attachment":[{"href":"https:\/\/javascriptguide.com\/wp-json\/wp\/v2\/media?parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javascriptguide.com\/wp-json\/wp\/v2\/categories?post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javascriptguide.com\/wp-json\/wp\/v2\/tags?post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}