Removing value from input - Jquery

I have this code:

 function updateTextArea() {

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

Whenever a user check a checkbox, a value is added to an INPUT field with the id=referralIds.

My question is, how can I do, so whenever the checkbox(es) are unchecked, the assigned value will be removed from the INPUT?

Thanks.

Upvotes: 1

Views: 132

Answers (1)

Rion Williams
Rion Williams

Reputation: 76547

Just set the value of your input area (referralIds) AFTER iterating through your checkboxes with the each() function, as shown:

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

Working Demo

Upvotes: 1

Related Questions