1

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.