How to show in separate lines/inputs(os), the values of an array every time the Checkbox is clicked?

Asked

Viewed 32 times

1

I have a problem with "how" to show the values received by the onClick event in the checkbox field, which are stored in an array. Every click on a checkbox stores its id in the array.

I can show all the values together while the user clicks on the checkbox using push, however I wanted to show them in separate lines and push would not serve in this case, tried some other ways and continues without the desired result.

function disciplina_escolhida(){
        var disciplina = [];
        $('#disciplina :checked').each(function () {
        disciplina.push($(this).val());
        $('#listas').append('<div><input type="text" name="campo" value='+disciplina+' disabled style="cursor:pointer"></div>');
    });
}

The code so far is this.

EDIT: My doubt is/was, as I should do to show separately the values that are stored in the Checkbox array, because every time someone clicks on a Checkbox, the values/names already selected previously, are added together with the new, making all values stick together on a single line, which was not the intention when showing the data of the same.

  • @Lienfeng your question is a little confused, please read the community guidelines on how to make a good question. Once you make the change and explain the situation better, giving examples, codes among other users and even I will be able to help you.

  • The push serves for you to insert elements in the array, not to make displays. Can explain your problem better?

  • Yes, about the push I expressed myself erroneously, but it was "this" even my problem, has already been answered but I will try to edit and leave in a more succinct way.

1 answer

1


Use the first parameter of the .each to pass the array value disciplina. This first parameter represents the index of the element in the .each, but first empty the div with .empty() not to repeat the fields:

function disciplina_escolhida(){
   $('#listas').empty();
   var disciplina = [];
   $('#disciplina :checked').each(function (i) {
      disciplina.push($(this).val());
      $('#listas')
      .append('<div><input type="text" name="campo" value='+disciplina[i]+' disabled style="cursor:pointer"></div>');
   });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Listas:
<div id="listas">
</div>
<div id="disciplina">
   <input type="checkbox" value="1">1
   <br>
   <input type="checkbox" value="2">2
   <br>
   <input type="checkbox" value="3">3
   <br>
   <input type="checkbox" value="4">4
</div>
<button onClick="disciplina_escolhida()">Adicionar</button>

  • I couldn’t understand his question correctly, if this is what he wants I’ll give you score.

  • That was my problem, when new values were added after the click on the checkbox they were always concatenated all together, thank you very much for the explanation and the code worked correctly.

  • @THIAGODEBONIS thanks! I think that’s right :D

Browser other questions tagged

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