The code takes the values of inputs
and sends to textarea
but when added more than titulo
the values are repeated in the result of the titles, for example, the descriptions of title 1 are the same as the title 2 vice versa, why does it happen? and how to make it work without changing the purpose?
Run the code in codepen.io or jsfiddle.net
What happens:
What should happen:
function resultado() {
var inp2 = document.getElementsByName("inp2");
var titu = document.getElementsByName("titu");
var res = document.getElementById("resultado");
res.value = "";
if (titu[0]) {
for (var k = 0; k < titu.length; k++) {
if (titu[k].value.trim() != '') {
res.value += '<div>
<span>${titu[k].value.trim()}</span>
</div>
<ul>\n';
for (var j = 0; j < inp2.length; j++) {
if (inp2[j].value.trim() != '') {
res.value += '<li>${inp2[j].value.trim()}</li>\n';
}
}
}
}
}else {
console.log("error")
res.value += '<ul>\n';
for (var l = 0; l < inp2.length; l++) {
if (inp2[l].value.trim() != '') {
res.value += '<li>${inp2[l].value.trim()}</li>\n';
}
}
}
};
// -----------------------------------------
let cont2 = 1;
document.getElementById("add2").onclick = function clone2() {
let container2 = document.getElementById("saida2");
let tempLinha2 = document.querySelector('#template2');
let clonar2 = document.importNode(tempLinha2.content, true);
const label2 = clonar2.querySelector("label");
label2.htmlFor = cont2;
clonar2.querySelector("input").className = cont2;
container2.appendChild(clonar2);
cont2++;
};
document.getElementById("del2").onclick = function del2() {
document.querySelector('#saida2 #linha2:last-child').remove();
};
// ---------------------------------------
let cont3 = 1;
document.getElementById("addtit").onclick = function clone3() {
let container3 = document.getElementById("saida2");
let tempLinha3 = document.querySelector('#template3');
let clonar3 = document.importNode(tempLinha3.content, true);
const label3 = clonar3.querySelector("label");
label3.htmlFor = cont3;
clonar3.querySelector("input").className = cont3;
container3.appendChild(clonar3);
cont3++;
document.getElementById('add2').id = 'add3';
document.getElementById('del2').id = 'del3';
};
document.getElementById("deltit").onclick = function deltit() {
document.querySelector('#saida2 #alg:last-child').remove();
document.getElementById('add3').id = 'add2';
document.getElementById('del3').id = 'del2';
};
// -----------------------------------------
if (document.getElementById("add3")) {
let cont4 = 1;
document.getElementById("add3").onclick = function clone4() {
let container4 = document.getElementById("saida3");
let tempLinha4 = document.querySelector('#template2');
let clonar4 = document.importNode(tempLinha4.content, true);
const label4 = clonar4.querySelector("label");
label4.htmlFor = cont4;
clonar4.querySelector("input").className = cont4;
container4.appendChild(clonar4);
cont4++;
};
document.getElementById("del3").onclick = function del4() {
document.querySelector('#saida3 #linha2:last-child').remove();
};
}
<div class="container">
<button id="addtit">+ TITULO</button>
<button id="deltit">- TITULO</button>
<button id="add2">+ DESCRIÇÃO</button>
<button id="del2">- DESCRIÇÃO</button>
<div id="saida2"></div>
<div class='botoes'>
<button onclick="resultado()" id='concluir'>CONCLUIR</button>
</div>
<div class="header"><span class="titulo">RESULTADO</span>
</div>
<div class="linha"><textarea id="resultado"></textarea>
</div>
</div>
<!-- template 2 -->
<template id="template2">
<div class="linha" id="linha2"><div class="coluna1"><label for="0">DESCRIÇÃO:</label></div><div class="coluna2"><input name="inp2" class="0" type="text"/></div>
</div>
</template>
<!-- template 3 -->
<template id="template3">
<div id="alg">
<div class="linha"><div class="coluna1"><label for="0">TITULO:</label></div><div class="coluna2"><input name="titu" class="0" type="text"/></div>
</div>
<div class="linha" id="linha3"><div class="coluna1"><label for="0">DESCRIÇÃO:</label></div><div class="coluna2"><input name="inp2" class="0" type="text"/></div>
</div>
<div id="saida3"></div>
</div>
</template>