Created
October 15, 2017 08:48
-
-
Save vince844/49ac5d6636734295c17d944ec18f79dd to your computer and use it in GitHub Desktop.
Gravity Form passing value from checkbox to text field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
var checkboxHandler = { | |
//Change these IDs to match your form | |
checkboxFieldId: 'input_1_25', //checkbox container ID | |
outputFieldId: 'input_1_23', //hidden field to capture values | |
checkboxField: null, | |
outputField: null, | |
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 | |
); | |
}, | |
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() ); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You can put this javascript snippet into a html field inside the gravity form |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment