I believe you are just starting to learn javascript, so I will give a detailed answer for your understanding.
you can easily remove element using the remove().
document.getElementById("#foo").remove();
Lets see how we can use that in our scenario. Your code has the following,
<input type="text" name="textBox0">
<input type="button" value="add" onclick="addInput()"/>
<input type="button" value="delete" onclick="deleteInput()"/>
One text type input element for user's input and tow button type input element which triggers a add and delete javascript functions to add/remove the text input and button input.
this
is a javascript keyword that holds the reference of the current object. We'll pass that as a parameter to our deleteInput() so in our function we know which element we are dealing with.
<input type="button" value="delete" onclick="deleteInput(this)"/>
Now in our function we'll receive that parameter as self. Our function now knows that it is dealing with the delete button input element. Simply call remove() on self.
function deleteInput(self) {
//code to remove element
self.remove();
}
But running that code will only remove the delete button. The reason being we are not telling the deleteInput() method we need to remove the text input and the other add button input as well. There is no way by which we can pass the reference of the other 2 input elements so what do we do?
Lets wrap our Input elements within a div
<div>
<input type="text" name="textBox0">
<input type="button" value="add" onclick="addInput()"/>
<input type="button" value="delete" onclick="deleteInput()"/>
</div>
parentElement() method gives back a reference to the parent of the element you call it upon, so calling self.parentElement() will give you the div's reference.
With that you can remove the entire div which accomplishes your goal.
function deleteInput(self) {
//code to remove element
self.parentElement().remove();
}
Please find the code and preview here: http://plnkr.co/edit/ROs6YJe4XeJTPFr2NBUT?p=preview