This post is an extension of Gravity Forms – Collecting Select Box Value.
In this demo we are capturing the values from a group of checkboxes.
Demo with Checkboxes
/**
* Wrap our JS for capturing values
*/
var checkboxHandler = {
//Change these IDs to match your form
checkboxFieldId: 'input_5_3', //checkbox container ID
outputFieldId: 'input_5_2', //hidden field to capture values
checkboxField: null,
outputField: null,
/*
Listen to Checkboxes
---------------------- */
init: function() {
this.outputField = document.getElementById(this.outputFieldId);
if( typeof this.outputField === 'undefined' || !this.outputField ) {
console.error('Missing output field ' + this.outputFieldId);
return;
}
this.checkboxField = document.getElementById(this.checkboxFieldId);
if(typeof this.checkboxField === 'undefined' || !this.checkboxField) {
console.error('Missing checkbox field ' + this.checkboxFieldId);
return;
}
jQuery(this.checkboxField).on(
'change',
'input[type=checkbox]',
{
checkbox: this.checkboxField,
output: this.outputField
},
this.setValues
);
},
/*
Set text (or hidden) field to list of checkbox values
----------------------------------- */
setValues: function(ev) {
var fields = ev.data;
var valueString = '';
jQuery(fields.checkbox).find('input:checked').each( function(i){
valueString += this.value + ', ';
});
fields.output.value = valueString.replace(/, $/, '');
}
};
jQuery().ready( checkboxHandler.init() );
If you are using a child theme or custom theme you can add this code to its JavaScript file.
Otherwise use WordPress’ wp_register_script and wp_enqueue_script functions to load a new file.
The value of the hidden/text box can then be included in your Gravity Forms notification.