1
I am using javascript to create inputs dynamically. I create a phone and email input this way.
I created a deleteEmail() function that deletes the latest created one, however when I create other fields I lose the reference from which to delete
What I want to know is how I delete these inputs created using pure JS?
function addEmail(){
var container = document.getElementById("box");
var input = document.createElement("input");
var label = document.createElement("label");
var btn_delete = document.createElement("button");
var br = document.createElement("br");
input.type = "text";
input.name = "email";
label.innerText = "Email";
label.htmlFor = "email";
btn_delete.type = "button"
btn_delete.innerText = "Deletar";
btn_delete.id = "btn-delete";
container.appendChild(br);
container.appendChild(btn_delete);
container.appendChild(input);
container.appendChild(label);
document.getElementById("btn-delete").onclick = function () {deleteEmail()};
function deleteEmail(){
btn_delete.remove();
input.remove();
br.remove();
label.remove();
}
}
Here’s the fiddle with the whole code https://jsfiddle.net/5munzeja/