0

I'm making a newsletter contact form, and in that form the user can check the subjects they want to receive newsletters from.

I want to make a text input where its value will be the value of all the checked boxes, separated with commas.

e.g.: retails, news, manufacturing

I want it to be dynamically, when the user checks a box it appears on the text input at the same moment, and when the user uncheck the box, it desappear at the same moment too, so I think jQuery is the easiest way, I just don't know how to do it.

Any ideas?

Thank you!

5
  • 1
    So where did you get stuck when you first made a start? Commented Jan 20, 2015 at 17:14
  • I know how to make it to get just one check box value, but not getting a lot of them and separated with commas Commented Jan 20, 2015 at 17:15
  • 1
    What have you tried so far? Where are you stuck? Do you have some code that you've worked on that's related to this? Commented Jan 20, 2015 at 17:15
  • possible duplicate of JQuery Add Multiple Check Action Values (of Checkboxes) to Input Text Commented Jan 20, 2015 at 17:16
  • Sorry, I really searched for the solution before asking but couldn't find it. But yes, it is duplicate :/ Commented Jan 20, 2015 at 17:37

4 Answers 4

1

Something like this? (see demo here)

$('input').on('change', function(){
   var res = "";

   $('input:checked').each(function(){
       res += $(this).val()+", ";
   });

   res = res.slice(0,-2);
   $('#result').text(res);
});
Sign up to request clarification or add additional context in comments.

1 Comment

With the necessary adaptations it worked like a charm! thank you!
1

something like this?

$(document).ready(function() {
    $('.check').on('change', function() {
        var displayVal = '';
        $('.check:checked').each(function() {
            displayVal += $(this).val() + ', ';
        });
        displayVal = displayVal.slice(0,-2);
        $('.display').val(displayVal);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" class="check" value="blue"/> blue<br/>
<input type="checkbox" class="check" value="red"/> red<br/>
<input type="checkbox" class="check" value="white"/> white<br/>
<br/>
<input type="text" class="display" />

Comments

0
$('#my-form').on('change', 'input[type="checkbox"]', function(){
    var vals = [];
    $('#my-form').find('input[type="checkbox"]').each(function(){
         if($(this).checked())){
             vals.push($(this).val());
         }
    });
    $('#my-input').val(vals.join(', '));
});

-- for example

1 Comment

I see what you did and unsderstand it, but unfortunately it didn't worked to me, I don't know why, but thanks for the help!
0

The following will return a string with the values of all checked boxes separated by commas.

var allChecked = $('input[type="checkbox"]:checked').map(function(){
    return this.value
}).get().join(', ');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.