Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mdailey77/28969f6c914368ab981a0a0471673f76 to your computer and use it in GitHub Desktop.
Save mdailey77/28969f6c914368ab981a0a0471673f76 to your computer and use it in GitHub Desktop.
Populate one hidden form field with multiple checkbox answers using jQuery
$('.customCheckboxesRow').each(function(){
var checkboxVal = '';
var checkboxel = $(this).find('.checkbox');
// checks if checkbox was selected, if so, adds rel attribute value of each checkbox to checkboxVal variable
checkboxel.each(function(){
if ($(this).hasClass('checkActive')){
var selectedRel = $(this).attr("rel");
checkboxVal += selectedRel + ', ';
}
});
checkboxVal = checkboxVal.slice(0, -2); //removes trailing comma
$(this).children('input').val(checkboxVal);
});
@jlipinski3
Copy link

jlipinski3 commented Apr 4, 2019

thanks for sharing Matt. Try this:

$('.customCheckboxesRow').each(function(){
var compiled_rel = $(".checkbox.checkActive[rel]", $(this)).map(function() {
return $(this).attr("rel");
}).get().join(", ");

$("[type=hidden]", $(this)).val(compiled_rel);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment