Is there a way to go back on original state after appending child inside it with Javascript?
This is how I append child to my div if the condition is met:
document.getElementById("myList1").appendChild("node");
And if the condition does not meet I want document.getElementById("myList1") go back to its normal state, without the appendChild, is there a way like this document.getElementById("myList1").reset();
I tried to parent.removeChild(child); but it removes all child of the parent, I only need to remove what was added by appendChild.
3
Answers
You can get the current HTML content of a selected element by using
innerHTML
on the element. You can reassign the savedinnerHTML
once you want to reset.If you want to remove all objects which are in the
<div>
use this code:Yes, you can go back to the original state of a element after appending a child using JavaScript. Before making any modifications, it’s important to maintain a copy of the original information or to refer back to it. Here’s an example that combines the two methods:
Approach 1: Cloning the Original Content
Approach 2: Keeping a Reference to the Original State
After adding a child element, you can restore the to its initial state using either method.