ExampleReplace a text node in an const newNode = document.createTextNode("Water"); element.replaceChild(newNode, element.childNodes[0]); Definition and UsageThe Syntaxnode.replaceChild(newnode, oldnode) ParametersParameterDescriptionnewnodeRequired.The node to insert.oldnodeRequired. The node to remove. Return ValueTypeDescriptionNodeThe replaced node.More ExamplesExampleReplace an // Create a new const element = document.createElement("li"); // Create a new text node: // Append the text node to the element.appendChild(textNode); // Get the
const list = document.getElementById("myList"); // Replace the first child node with the new Browser Support
It is fully supported in all browsers: ChromeEdgeFirefoxSafariOperaIEYesYesYesYesYes9-11Setelah kita membuat elemen pada Javascript dengan createElement maka pembelajaran Javascript kita sampai kepada replaceChild dan removeChild. Seperti namanya replaceChild ini berarti parent akan melakukan penggantian child, sedangkan removeChild ini berarti parent akan menghapus child. Kita akan membuat source code HTML, source code Javascript dan tampilan yang dihasilkan. Source code HTML yang akan kita gunakan adalah seperti berikut ini:
Source code Javascriptnya adalah seperti berikut ini: //buat sayuran let sayuran = document.createElement('li'); sayuran.innerHTML = 'Sayuran'; menu3 = document.querySelector('#menu3'); let makan_siang = document.querySelector('#makan_siang'); //ganti menu3 dengan sayuran makan_siang.replaceChild(sayuran,menu3); //menu4 itu Soda Dingin menu4 = document.querySelector('#menu4'); makan_siang.removeChild(menu4); Hasilnya dapat dilihat di https://js.aris.proweb.asia/3-7-1-replace-remove.html dengan tampilan seperti berikut ini: Informasi lebih lanjut silahkan mengunjungi: Kunjungi www.proweb.co.id untuk menambah wawasan anda . ReplaceChild dan removeChild pada Javascript arisg August 20, 2019August 20, 2019 Javascript Metode Sintaks: Node.replaceChild(newChild, oldChild)
Metode Contoh:
Coba jalankan demo di bawah ini: Ganti ‘Mango’ dengan ‘Banana’Demikian yang dapat kami share kepada sobat source code aplikasi pada kesempatan ini, semoga dapat bermanfaat dan bisa menjadi referensi pemrograman bagi anda. Jangan lupa like Fan Page kami, dan SUBSCRIBE Channel Youtube kami untuk dapatkan update source code aplikasi terbaru. The DOMNode::replaceChild() function is an inbuilt function in PHP which is used replace the old child node with the passed new node. Further, if the new node is already a child it will not be added the second time. If the replacement succeeds the old node is returned. Syntax: DOMNode DOMNode::replaceChild( DOMNode $newnode, DOMNode $oldnode ) Parameters: This function accept two parameters as mentioned above and described below:
Return Value: This function returns old node or FALSE if an error occur. Exceptions: This function throws DOM_NO_MODIFICATION_ALLOWED_ERR, if this node is read-only or if the previous parent of the node being inserted is read-only, DOM_HIERARCHY_REQUEST_ERR, if this node is of a type that does not allow children of the type of the $newnode node, or if the node to put in is one of this node’s ancestors or this node itself, DOM_WRONG_DOCUMENT_ERR, if $newnode was created from a different document than the one that created this node, DOM_NOT_FOUND, if $oldnode is not a child of this node. |