Removing Elements

In JavaScript, an element can only be deleted from its parent. To delete one, you have to get the element, find its parent, and delete it using the removeChild method.[1]

For example, in a HTML document that looks like

<div id="parent">
    <p id="child">I'm a child!</p>
</div>

The code you would need to delete the element with the ID "child" would be

// get elements
var child = document.getElementById("child");
var parent = document.getElementById("parent");

// Delete child
parent.removeChild(child);

Instead of getting the parent element manually, you can use the parentNode property of the child to find its parent automatically. The code for this on the above HTML document would look like

// Get the child element node
var child = document.getElementById("child");

// Remove the child element from the document
child.parentNode.removeChild(child);

References



  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.

JavaScript/Removing_elements
 



 

Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.


Manage research, learning and skills at defaultlogic.com. Create an account using LinkedIn to manage and organize your omni-channel knowledge. defaultlogic.com is like a shopping cart for information -- helping you to save, discuss and share.


  Contact Us