Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Remove Child Element without Knowing its Parent</title> </head> <body> <div id="main"> <h1 id="title">Hello World!</h1> <p id="hint">This is a simple paragraph.</p> </div> <button type="button" onclick="removeElement()">Remove Element</button> <script> function removeElement() { let childElem = document.getElementById("hint"); childElem.parentNode.removeChild(childElem); } </script> </body> </html>