When I add a few of <select>
ex. SELECT_country, SELECT_country1, SELECT_country2
I would like to remove them in the order of appearance from the newest one to oldest one. But it is removing from oldest one to newest one. I thought SELECT_country
is parent, and I will be deleting its child, but the parent goes away first.
How I can change it?
var j = 0;
function add_country() {
j++;
var select = document.getElementById("SELECT_country");
var clone = select.cloneNode(true);
clone.setAttribute("id", "SELECT_country" + j);
clone.setAttribute("name", "country" + j);
document.getElementById("DIV_country").appendChild(clone);
}
function remove_country() {
var select = document.getElementById('SELECT_country');
select.parentNode.removeChild(select);
}
First, get the ul element with the id menu by using the getElementById() method. Then, remove the last element of the ul element by using the removeChild() method. The menu. lastElementChild property returns the last child element of the menu .
removeChild() The removeChild() method of the Node interface removes a child node from the DOM and returns the removed node. Note: As long as a reference is kept on the removed child, it still exists in memory, but is no longer part of the DOM. It can still be reused later in the code.
Child nodes can be removed from a parent with removeChild(), and a node itself can be removed with remove(). Another method to remove all child of a node is to set it's innerHTML=”” property, it is an empty string which produces the same output.
You can select the parent element and use the lastChild property. To anyone using lastChild experiencing issues. Try . lastElementChild , it ignores text and comment nodes which could be in the way.
The removeChild () returns the removed child node from the DOM tree but keeps it in the memory, which can be used later. If you don’t want to keep the removed child node in the memory, you use the following syntax:
Another method to remove all child of a node is to set it’s innerHTML=”” property, it is an empty string which produces the same output. This method is not preferred to use. Example-1: Using “removeChild ()”.
More "Try it Yourself" examples below. The lastChild property returns the last child node of the specified node, as a Node object.
The Element.lastElementChild read-only property returns an element's last child Element, or null if there are no child elements. Element.lastElementChild includes only element nodes. To get all child nodes, including non-element nodes like text and comment nodes, use Node.lastChild.
Since you are appending new elements you need to get the last element by using lastChild
if you want to remove them from newest to oldest.
function remove_country() {
var select = document.getElementById('DIV_country');
select.removeChild(select.lastChild);
}
JSFiddle Demo: https://jsfiddle.net/rrkroxcb/1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With