0

I currently have the following code which adds a new input box everytime the user clicks a button. Is there a way to create a button to remove the last generated input box?

var data = '<label>Temperature (K):</label><input type="number" name="temp"/>'


function addNew() {
  var newContent = document.createElement('div');
  newContent.innerHTML = data;
  document.getElementById('target').appendChild(newContent);
}
<div id="target"></div>

<input id="add" type="button" value="Add New" onclick="addNew()" />

2

7 Answers 7

1

Push your inputs into an array:

inputs=[];

inputs.push(newContent);
//put this in your addNew function

Now you can get the last element out of that array and remove it

function remove(){
if(inputs.length>0){
elem=inputs.pop();
elem.parentNode.removeChild(elem);
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

This will throw an error if the inputs length is 0.
0

Add:

<input id="remove" type="button" value="Remove last element" onclick="removeLastElem()" />

And:

function removeLastElem() {
    document.getElementById('target').lastChild.remove()
}

EXAMPLE :)

Comments

0

Here is sample of your java-script to remove added div

First you need to create button to remove newly added row and onclick you need to remove its parent element(last added div).

 var data='<label>Temperature (K):</label><input type="number" name="temp"/> <input type="button" value="Remove" onclick="removeDiv(this)"  />';


function addNew() {
   var newContent = document.createElement('div');
  newContent.innerHTML = data;
  document.getElementById('target').appendChild(newContent);
  }

function removeDiv(args){
args.parentNode.remove()
}

Sample JSBIN

Comments

0

Simple select last child and remove it

function removeLast(){
 document.getElementById("target").lastChild.remove(); 
}

Here is jsfiddle

https://jsfiddle.net/aqev0pu4/

Comments

0

Put all the content you add to the form in a wrapping node:

var data = '<span><label>Temperature (K):</label><input type="number" name="temp"/></span>'

You can remove the last child of your "target" node:

function removeLast() {
    document.getElementById("target").lastChild.remove();
}

2 Comments

Can't you wrap it in a container node like: var data = '<span><label>Temperature (K):</label><input type="number" name="temp"/></span>'.
Yes shure, but you may need to describe it in your answer
0

Just keep a list of added elements :

var data = '<label>Temperature (K):</label><input type="number" name="temp"/>'

var addedList = []; 
function addNew() {
  var newContent = document.createElement('div');
  newContent.innerHTML = data;
  addedList.push(document.getElementById('target').appendChild(newContent));
}
function removeLast() {
    if (addedList.length) {
        document.getElementById('target').removeChild(addedList.pop());
    }
}
<div id="target"></div>

<input id="add" type="button" value="Add New" onclick="addNew()" />
<input id="remove" type="button" value="Remove last added" onclick="removeLast()" />

If you just want the last element and not a list :

var data = '<label>Temperature (K):</label><input type="number" name="temp"/>'

var addedEl = null; 
function addNew() {
  var newContent = document.createElement('div');
  newContent.innerHTML = data;
  addedEl = document.getElementById('target').appendChild(newContent);
}
function removeLast() {
    if (null !== addedEl) {
        document.getElementById('target').removeChild(addedEl);
        addedEl = null;
    }
}
<div id="target"></div>

<input id="add" type="button" value="Add New" onclick="addNew()" />
<input id="remove" type="button" value="Remove last added" onclick="removeLast()" />

Comments

0

Keep track of last inserted input tag with a counter as sub-string of its id and remove the last one by reading the id using counter.

var inputCounter = 1;

function addNew() {
	var data = '<label>Temperature (K):</label><input type="number" id="counter' + inputCounter + '" name="temp"/>';
	inputCounter++;

	var newContent = document.createElement('div');
	newContent.innerHTML = data;
	document.getElementById('target').appendChild(newContent);
}

function removeLast() {
	if (inputCounter > 1) {
		inputCounter--;
		var last = document.getElementById("counter" + inputCounter).parentElement;
		document.getElementById('target').removeChild(last);
	} else {
		alert("Add new inputs. No last inserted input found.")
	}
}
<div id="target"></div>

<input id="add" type="button" value="Add New" onclick="addNew()" />
<input id="remove" type="button" value="Remove last added" onclick="removeLast()" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.