Show all input checkbox marked but with a jquery line break

0

I have this

<input><?php echo $elementos?><input>
...


function updateTextArea() {  

   var allVals = [];

   $('input:checked').each(function() {

      allVals.push($(this).attr("id"));

   });

 $('div#choose').html(allVals+"<br/>")

};
$(function() { 

    $('input').click(updateTextArea);

    updateTextArea();

});

Good morning. I have this code that recycles it from this page. It works perfectly but it does not come with line break in my ( div#choose ) and I do not know how to place it so that each element with its line break of that array comes out.

    
asked by Jesus Garcia Barca 20.02.2018 в 11:28
source

1 answer

3

The problem is that with your code you add a single line break ( <br/> ) to the end of the data to be displayed. You should include a line break for each element.

The simplest way is to use the method join of the object Array that allows you to convert an array into a string indicating a separator to enter between each element (in this case it would be the line break):

 $(function(){
  var allVals = ['elemento1', 'elemento2', 'elemento3'];
  $('div#choose').html(allVals.join('<br/>'));
 });
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="choose"></div>
    
answered by 20.02.2018 / 11:34
source