Good morning to all ... My goal is to put in an array the data that goes putting two inputs of my form, with the exception that when pressing the button more, the information is saved in an array, and it is created another new element "box / div", two more inputs to fill with its two corresponding button. And if I give the minus button, delete the information from that record in the array.
The problem I have is the following: I have managed to create the two inputs plus the two "+" and "-" buttons and eliminate the inputs and buttons. I can not put information in the array, only the first box of inputs, the following boxes I can not put the information in the array. I leave my code for you to take a look and I hope someone helps me. Thanks!
HTML FILE:
//cargamos el documento, sin el contenido multimedia
document.addEventListener("DOMContentLoaded",main);
function main(){
var formacion = new Array();
//recogemos en un array los datos que haya en formacion
function recogerFormacion(){
var anno = document.getElementById("year").value;
var curso = document.getElementById("curso").value;
var lista = document.getElementById("listaFormacionFinal");
formacion.push(anno+" "+curso);
console.log(formacion);
var datos = document.createElement("li");
datos.innerHTML = anno+" "+curso;
lista.appendChild(datos);
}
//function para el botón "+"
function anadirCaja(){
var divPrimero = document.getElementById("primero");
var divResto = document.createElement("DIV");
divPrimero.append(divResto);
divResto.setAttribute("class", "visible");
divResto.setAttribute("id", "primero");
divResto.setAttribute("name", "cajas");
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","+");
inputBotonMas.addEventListener("click", anadirCaja);
inputBotonMenos.addEventListener("click",quitarCaja);
divResto.append(inputAnno);
divResto.append(inputCurso);
divResto.append(inputBotonMenos);
divResto.append(inputBotonMas);
recogerFormacion();
}
//funcion para el boton "-"
function quitarCaja(){
var cajas = document.getElementsByName("cajas");
var cajaEliminar = document.getElementById("segundo");
var totalCajas = cajas.length;
console.log(totalCajas);
if(totalCajas == 1){
alert("No se puede eliminar");
} else {
cajaEliminar.parentNode.removeChild(cajaEliminar);
}
}
//Ocultamos el formulario y mostramos los datos recogidos
function mostrarDatosFinales(){
if(recogerFormacion()){
var ocultarFormulario = document.getElementById("contenedor");
ocultarFormulario.className="oculto";
var mostrarDatos = document.getElementById("datosFinales");
mostrarDatos.className="visibleFinal";
}
}
//boton de reset: blanqueamos los campos
function borrarDatos(){
document.forms.prueba.nombre.value ="";
document.forms.prueba.apellidos.value ="";
document.forms.prueba.dni.value ="";
document.forms.prueba.correo.value ="";
document.forms.prueba.year.value ="";
document.forms.prueba.curso.value ="";
document.forms.prueba.textarea.value ="";
}
var boton = document.getElementById("boton");
boton.addEventListener("click",mostrarDatosFinales);
var botonr = document.getElementById("botonb");
botonr.addEventListener("click",borrarDatos);
var mas = document.getElementById("mas");
mas.addEventListener("click",anadirCaja);
var menos = document.getElementById("menos");
menos.addEventListener("click",quitarCaja);
}
#contenedor{
width: 75%;
margin: 0 auto;
}
input{
display: block;
border-radius: 3px;
}
#boton{
margin-top: 5px;
}
#contenedor > #formulario >p{
font-size: 25px;
font-family: Courier, "Lucida Console", monospace;
font-weight: bold;
}
#primero > input {
display: inline;
border-radius: 3px;
margin: 2px;
}
input#mas{
margin-left: 15px;
border-radius: 7px;
}
input#menos{
margin-left: 15px;
border-radius: 7px;
}
.oculto{
display:none;
}
.visibleFinal p{
display:inline;
width: 75%;
margin-left: 0px;
}
#primero > input {
display: inline;
border-radius: 3px;
margin: 3px;
}
#primero > input#mas{
margin-left: 18px;
border-radius: 7px;
}
#primero > input#menos{
margin-left: 17px;
border-radius: 7px;
}
#datosFinales{
width: 75%;
margin: 0 auto;
}div > div {
display: inline-block;
}
<!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" class="visible">
<form name="prueba" action="#" id="formulario">
<p>Formacion:</p>
<div class="visible" id="primero" name="cajas">
<label for="year"></label>
<input type="text" id="year" size="2" name="year">
<label for="curso"></label>
<input type="text" id="curso" size="50" name="curso">
<input type="button" id="menos" value="-">
<input type="button" id="mas" value="+">
</div>
<button type="button" id="boton" value="validar">validar</button>
<button type="button" id="botonb" value="borrar">Borrar</button>
</form>
</div>
<div id="datosFinales" class="oculto">
<p>Formacion:</p>
<ul id="listaFormacionFinal"></ul>
</div>
</body>
</html>