I have the following problem: I have a form that has 4 inputs, two of them are for writing, and the other two are two buttons, one button is "+" and the other "-". The objective is that pressing the "+" button creates another 4 inputs, and if the "-" button is given, those 4 inputs will be eliminated.
So far I have only been able to paint the inputs from the first div I have, the buttons that are created have no function. I need to associate the first event and that the elements that it creates, use that same event. I need to do it only in javaScript, even though I have looked at it I can not find the solution, I would appreciate it very much
I show you what I have:
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Práctica a Entregar</title>
<meta charset="utf-8">
<script type="text/javascript" src="1.js"></script>
<link rel="stylesheet" type="text/css" href="practica1.css">
</head>
<body>
<h1>Solicitud de empleo</h1>
<div id="contenedor">
<form name="prueba" action="#" id="formulario">
<p>Formacion:</p>
<div class="test" id="primero">
<input type="text" id="year" size="2">
<input type="text" id="curso" size="50">
<input type="button" id="menos" value="-">
<input type="button" id="mas" value="+">
</div>
</form>
</div>
</body>
</html>
File js:
document.addEventListener("DOMContentLoaded",main);
function main(){
function anadirCaja(){
var divPrimero = document.getElementById("primero");
var divResto = document.createElement("DIV");
divPrimero.append(divResto);
divResto.setAttribute("class", "test");
divResto.setAttribute("id", "primero");
var inputAnno = document.createElement("input");
var inputCurso = document.createElement("input");
var inputBotonMenos = document.createElement("input");
var inputBotonMas = document.createElement("input");
inputAnno.setAttribute("type","text");
inputAnno.setAttribute("id","year");
inputAnno.setAttribute("size","2");
inputCurso.setAttribute("type","text");
inputCurso.setAttribute("id","curso");
inputCurso.setAttribute("size","50");
inputBotonMenos.setAttribute("type","button");
inputBotonMenos.setAttribute("id","menos");
inputBotonMenos.setAttribute("value","-");
inputBotonMas.setAttribute("type","button");
inputBotonMas.setAttribute("id","mas");
inputBotonMas.setAttribute("value","+");
divResto.append(inputAnno);
divResto.append(inputCurso);
divResto.append(inputBotonMenos);
divResto.append(inputBotonMas);
}
var mas = document.getElementById("mas");
mas.addEventListener("click",anadirCaja);
var menos = document.getElementById("menos");
menos.addEventListener("click",quitarCaja);
}