Using jQuery to get multiple checkbox's value and output as comma separated String.
Use the .map()
function:
$('.Checkbox:checked').map(function() {return this.value;}).get().join(',')
Breaking that down:
$('.Checkbox:checked')
selects the checked checkboxes.
.map(function() {return this.value;})
creates a jQuery object that holds an array containing the values of the checked checkboxes.
.get()
returns the actual array.
.join(',');
joins all of the elements of the array into a string, separated by a comma.
Working DEMO
var areaofinterest = '';
$('[name="areaofinterest"]').each(function(i,e) {
var comma = areaofinterest.length===0?'':',';
areaofinterest += (comma+e.value);
});
To get only checked boxes value :
var areaofinterest = '';
$('[name="areaofinterest"]').each(function(i,e) {
if ($(e).is(':checked')) {
var comma = areaofinterest.length===0?'':',';
areaofinterest += (comma+e.value);
}
});
FIDDLE
You could also do:
var areaofinterest = [];
$('[name="areaofinterest"]:checked').each(function(i,e) {
areaofinterest.push(e.value);
});
areaofinterest = areaofinterest.join(',');
And there's probably a bunch of other ways to do this ?