Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html> <head> <title>Replace Node Example</title> </head> <body> <div id="oldElement">This element will be replaced. <button onclick="replaceElement()">Replace</button></div> <script> function replaceElement() { let newNode = document.createElement('div'); newNode.textContent = 'This is a replacement.'; let oldNode = document.getElementById('oldElement'); oldNode.parentNode.replaceChild(newNode, oldNode); } </script> </body> </html>