Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
How do you make dynamic input element count based on variable integer input?

So, I am not all to versed in JavaScript so forgive me for what will probably be an easy answer. In JavaScript, not JQuery, I am trying to create a script to change an input boxes value with 2 buttons, up and down. That, works. The next step was to take the value and create multiple input buttons with the type="file". I have tried multiple versions of this but i cant seem to get it working right. Any input would be helpful..

<div id="groupConfig" class="row"></div>
<input type="number" class="number" id="groups" value="1" onchange="layersElement()">
<input type="button" value="down" class="button" id="selNumDown" onclick="selDown()">
<input type="button" value="up" class="button" id="selNumUp" onclick="selUp()"><br>

<div id="numGroups"></div>

const numGroups = document.getElementById("groupConfig");
const numSelected = document.getElementById("groups");

function selUp(){
    if((numSelected.value >= 1) && (numSelected.value < 10)) {
        numSelected.value++;
    }else{
        numSelected.value = 10;
    }
    numGroups.appendChild(numSelected);
}
function selDown(){
    if((numSelected.value <= 10) && (numSelected.value > 1)) {
        numSelected.value--;
    }else{
        numSelected.value = 1;
    }
    numGroups.appendChild(numSelected);
}

// THIS IS THE PART I NEED HELP WITH VVV

function layersElement() {
    let numLayerResult = document.getElementById("groups").value;

    const numLayers = document.getElementById("numGroups");

    for (let x = 1; x <= numLayerResult; x++) {
        const fileLayerGroups = document.createElement("input");
        fileLayerGroups.type = "file";
        fileLayerGroups.class = "file";

        let idNum;
        idNum = "Layer" + x;
        fileLayerGroups.id = idNum;

        numLayers.appendChild(fileLayerGroups);
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There were two problems with your code.

The first one was, that the layersElement function was not called on the click of a button. And the second one was that you didnt remove all of the created file inputs. I added the following line:

numLayers.innerHTML = ''

This clears all of the children.

const numGroups = document.getElementById("groupConfig");
const numSelected = document.getElementById("groups");

function selUp(){
    if((numSelected.value >= 1) && (numSelected.value < 10)) {
        numSelected.value++;
    }else{
        numSelected.value = 10;
    }
    numGroups.appendChild(numSelected);
    layersElement()
}
function selDown(){
    if((numSelected.value <= 10) && (numSelected.value > 1)) {
        numSelected.value--;
    }else{
        numSelected.value = 1;
    }
    numGroups.appendChild(numSelected);
    layersElement()
}

function layersElement() {
    let numLayerResult = document.getElementById("groups").value;

    const numLayers = document.getElementById("numGroups");
    
    //clear all of the file uploads
    numLayers.innerHTML = ''

    for (let x = 1; x <= numLayerResult; x++) {
        const fileLayerGroups = document.createElement("input");
        fileLayerGroups.type = "file";
        fileLayerGroups.class = "file";

        let idNum;
        idNum = "Layer" + x;
        fileLayerGroups.id = idNum;

        numLayers.appendChild(fileLayerGroups);
    }
}
<div id="groupConfig" class="row"></div>
<input type="number" class="number" id="groups" value="1" onchange="layersElement()">
<input type="button" value="down" class="button" id="selNumDown" onclick="selDown()">
<input type="button" value="up" class="button" id="selNumUp" onclick="selUp()"><br>

<div id="numGroups"></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda