Janabl
Janabl

Reputation: 7

Checkboxes in while loop won't give value

I'm trying to get all the values of my selected checkboxes into a textarea. The checkboxes and their values are taken from a DB. I don't know why, but the only thing my textarea is giving me is the word 'on' when something is selected.

these are the checkboxes:

 <div class="controls" id="c_b">
        <?php
            echo "<ul>";
            while($user = $allUsers->fetch_assoc())
            {
                echo "<li><input type='checkbox'> " . $user['username'] . " </input></li>";
            }
            echo "</ul>";
            ?>
            <br />
            </div>

and this is the function:

function updateTextArea() {         
     var allVals = [];
     $('#c_b :checked').each(function() {
       allVals.push($(this).val());
     });
     $('#t').val(allVals)
  }
 $(function() {
   $('#c_b input').click(updateTextArea);
   updateTextArea();
 });

thanks in advance, I hope somebody can see what I'm missing! Jana

Upvotes: 0

Views: 385

Answers (3)

mistapink
mistapink

Reputation: 1956

You may add the attribute value to the checkbox: http://jsfiddle.net/RNdFS/

Upvotes: 1

MrCode
MrCode

Reputation: 64526

If you don't set a value on a checkbox, it will default to on if checked. You need to set the value to each username.

<input type="checkbox" value="bob" />

Upvotes: 2

thecodeparadox
thecodeparadox

Reputation: 87073

You can try

     $('#c_b input[type=checkbox]:checked').each(function() {
       allVals.push($(this).val());
     });

Upvotes: 0

Related Questions