Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS remove last child?

Tags:

javascript

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);
}
like image 362
slk500 Avatar asked Dec 10 '15 04:12

slk500


People also ask

How do I get rid of my last child?

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 .

What is removeChild in JavaScript?

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.

How do you remove an element from a child?

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.

How do I get the last child in JavaScript?

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.

What is the use of removechild in JavaScript?

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:

How to remove all the children of a node in JavaScript?

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 ()”.

What is the Lastchild property in JavaScript?

More "Try it Yourself" examples below. The lastChild property returns the last child node of the specified node, as a Node object.

How do I get the last child node of an element?

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.


1 Answers

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/

like image 167
Miguel Mota Avatar answered Oct 11 '22 08:10

Miguel Mota