Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Help With Javascript Loop

mcfc4heatons

New Coder
Hello - I want to take the filename from a file input and then populate in some text inputs, doesn't work correctly and only one text input is populated:

Code:
<html>
<head>
<meta charset="utf-8">
</head>

<body>
    <form>
    <input type="file" id="fileinput"><br>
    <input type="text" id="textinput_1" class="countme"><br>
    <input type="text" id="textinput_2" class="countme"><br>
    <input type="text" id="textinput_3" class="countme"><br>
    <input type="text" id="textinput_4" class="countme">
    </form>
    <script>

const countAll = document.querySelectorAll('.countme').length; // Count elements
console.log(countAll); //  3
for (let i = 1; i < countAll; i++) {

  const textinput = document.getElementById('textinput_' + i); // textinput_1, textinput_2, etc
  document.getElementById('fileinput').oninput = function() {
    textinput.value = document.getElementById('fileinput').files[0].name;
        
  }
}
    </script>
</body>
</html>
 
Indeed it only populates the third textfield.
Why the third ? Because you miss the last element in your loop. The condition should be i <= countAll.
If you correct that, you'll see it now only populates the last textfield.
Why only the last ? Because you define the oninput function four times inside your loop. So only the last definition (which is for the 4th element) sticks.

You need to define the function once, and place the loop inside it:

JavaScript:
document.getElementById('fileinput').oninput = function()
{
    for (i = 1; i <= countAll; i++)
    {
        const textinput = document.getElementById('textinput_' + i); // textinput_1, textinput_2, etc
        textinput.value = document.getElementById('fileinput').files[0].name;
    }
}

That will work. But note that you should always start loops from zero, not one.
Or much better still, get rid of the loop counter and simply write

JavaScript:
document.getElementById('fileinput').oninput = function()
{
    for (t of document.querySelectorAll('.countme'))
    {
        t.value = document.getElementById('fileinput').files[0].name;
    }
}
Now you're also not dependent on the name of the textinput elements.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom