Home > other >  How can generate numbers from given number and then populate in while loop
How can generate numbers from given number and then populate in while loop

Time:11-27

i'm trying to generate numbers from from_number value and then populate in a loop

Let's say that from_number value is 10 `

<label>
    <input type="text" 
           id="from_number" placeholder="from_number"
           autocomplete="off">
</label>

start php while.. I have 50 inputs with the same name ['list']

<label>
    <input type="text"  name="list"
           placeholder="list" autocomplete="off">
</label>

` end php

`

$('#from_number').blur(function () {
    let begin = document.getElementById('from_number').value;
    let arr = [];
    let inputs = document.querySelectorAll('[name^="list"]');
    for (let i = 0; i < inputs.length; i  ) {
        inputs[i].value = arr.push(begin  )   begin -1;
    }
});

`

Thanks

DESIRED OUTPUT

`

<input type="text"  name="list" value="10" autocomplete="off">
<input type="text"  name="list" value="11" autocomplete="off">
<input type="text"  name="list" value="12" autocomplete="off">
<input type="text"  name="list" value="13" autocomplete="off">
<input type="text"  name="list" value="14" autocomplete="off">
<input type="text"  name="list" value="50" autocomplete="off">

`

CodePudding user response:

The document.querySelectorAll() method returns a NodeList object container which can be iterated over as an array. The event listener in the following code fragment checks that the input value is numeric before converting it to an integer and iterating over the array of inputs.

const startValueElem = document.getElementById("from_number");

startValueElem.addEventListener("blur", (event) => {
   if (!isNaN(event.target.value)) {
      let value = parseInt(event.target.value);
      Array.from(document.querySelectorAll('[name^="list"]')).forEach(el => {
         el.value = value  ;
      });
   }
});
  • Related