
How to Add values of checkboxes to the input text field?
We stored the checkboxes value in a variable $checks
, after that attach the handler to this collection. Inside the event handler, I take the collection once again and filter (return) only the checkboxes that are checked.
Jquery
$(document).ready(function(){
$checksbx = $(":checkbox");
$checksbx.on('change', function() {
var string = $checksbx.filter(":checked").map(function(i,v){
return this.value;
}).get().join(" ");
$('#results').val(string);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="results"/><br>
<input type="checkbox" value="1">1<br>
<input type="checkbox" value="2">2<br>
<input type="checkbox" value="3">3