0
I am very new with javascript and when it comes to creating loopings I get totally lost, the code below is to create the elements with each click and number the id
and the text, but I’m having difficulties about it, I managed to, solve with an input and adding the amount, but that’s not the result I need, how should I proceed? If possible explain the amendments, Thank you.
document.getElementById("add1").onclick = function clone(){
var qt = 1;
var container = document.getElementById("saida1");
while (container.hasChildNodes()) {
container.removeChild(container.lastChild);
}
for (i=0;i<qt;i++){
var input = document.createElement("input");
var div1 = document.createElement("div");
var div2 = document.createElement("div");
var div3 = document.createElement("div");
var label = document.createElement("label");
div1.className = "linha";
div2.className = "coluna1";
div3.className = "coluna2";
label.htmlFor = (i+1)
label.textContent = "Texto " + (i+1) + ": ";
input.className = (i+1);
input.name = "inp";
input.type = "text";
container.appendChild(div1);
div1.appendChild(div2);
div2.appendChild(label);
div1.appendChild(div3);
div3.appendChild(input);
}
}
<div id="saida1"></div>
<button id="add1">ADD</button>
first, if the variable "Qt" is always 1, this command
for (i=0;i<qt;i++){
is purposeless, will always do everything once, it is unnecessary. Second, this line removes the items you already have in the div "container":container.removeChild(container.lastChild);
then only 1 element will appear always. Finally, you would have to count how many elements you have in the div "container" in order to be able to number– Ricardo Pontual
i forgot to remove
...container.removeChild(container.lastChild);...
when I made the code using input to get the values– Mark Vaaz