0

good evening, I've been trying since yesterday to figure out how to make my script work correctly but after several attempts it still does not work, the error is attributed to the sum that is displayed on the individual records, instead of being added, it is placed side by side. How can I correct it?

function addFields(){
            var number = document.getElementById("copie").value;
            var numeroiniziale = document.getElementById("inizio").value;
            var container = document.getElementById("container");
            var risultato = parseInt("inizio");
            while (container.hasChildNodes()) {
            container.removeChild(container.lastChild);
        }



        for (i=0;i<number;i++){
            container.appendChild(document.createElement("br"));
            container.appendChild(document.createTextNode("N° " + (i+1)));
            container.appendChild(document.createElement("br"));
            var input = document.createElement("input");
            input.type = "text";
            input.placeholder = (risultato) + (i+1);
            container.appendChild(input);
            container.appendChild(document.createElement("br"));
        }
    }
Numero iniziale:<input type="text" id="inizio" name="inizio" value=""><br /> Quanti ne genero:<input type="text" id="copie" name="copie" value=""><br />
<a href="#" id="filldetails" onclick="addFields()">Genera</a>
<div id="container"></div>
Sklero Mc
  • 1,365
  • 2
  • 10
  • 9
  • Convert `inizio` to a number before adding it? `"1" + 1 == "11"` but `parseInt("1", 10) + 1 == 2`. The final conversion to a number is coming too late. – Dave Newton Jun 15 '22 at 17:07
  • ok, i have insert var risultato = parseInt("inizio"); and to placehold this input.placeholder = (risultato) + (i+1); now give me NaN to result i updated the script – Sklero Mc Jun 15 '22 at 17:30
  • `risultato` is currently scoped to `addFields` and is not available outside that function. – Dave Newton Jun 15 '22 at 17:38

0 Answers0