Take input value to run in the for loop

Asked

Viewed 98 times

0

I have the input QtdParcela, and I want my loop generates data up to the value entered in input.

Follow part of my code:

In the for, where is 10, I want you to receive the value that the user will enter. How do I do this?

<div class="form-group col-md-3">
      <label for="QtdParcela">Quantidade Parcela</label>
      <input type="text" name="QtdParcela" id="QtdParcela" class="form-control">
</div>  

<button type="button" id="bt_add" class="btn btn-primary">Adicionar</button>

<script>

$(document).ready(function(){
      $('#bt_add').click(function(){
            adicionar();

      });
});

function adicionar(){

      for ($i = 0; $i < 10; $i++)

      endfor
}

</script>

2 answers

0

No no where is 10, I want to put the value that the user will put, how do I do this?

Just pass the value of input as a parameter for your function:

<script>

    $(document).ready(function(){
        $('#bt_add').click(function(){
            let parcelas = $('#QtdParcela').val();
            adicionar(parcelas);
        });
    });

    function adicionar(param1){
        for ($i = 0; $i < param1; $i++)

        endfor
    }

</script>

PS: I ignored the rest of the code!

0

The previous code did not work, but could solve. I forgot to open and close {}

 for ($i = 0; $i < param1; $i++){

 }
 endfor
  • In that case you don’t use the endfor! Keys already delimit the loop...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.