I\'d like to populate a hidden input with the values of selected checkboxes with a space between these values. I\'ve tried the following which in theory should work but it i
:checkbox is deprecated
Instead of
$(':checkbox:checked')
Use
$('input[type="checkbox"]:checked')
Also none of the checkboxes are checked when the document is Ready .. Set them first and then try ...
HTML
Javascript
function Populate(){
vals = $('input[type="checkbox"]:checked').map(function() {
return this.value;
}).get().join(',');
console.log(vals);
$('#tags').val(vals);
}
$('input[type="checkbox"]').on('change', function() {
Populate()
}).change();
Check Fiddle