Created
February 13, 2024 18:15
-
-
Save Garconis/671d51dfd6b1dd7d42a1b3abfd83afea to your computer and use it in GitHub Desktop.
Gravity Forms | Get selected dropdown choice text and value, then pull out first name and prefill into hidden field input
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
// Get dropdown info from Gravity Form PM selections | |
// After the Gravity validates the fields, the Gravity form will re-render all the fields, so the script code you write will not be bind to the new element. | |
// So you need to bind it again (after validation) or use this hook gform_post_render. | |
jQuery(document).on('gform_post_render', function( ){ | |
// and especially whenever it's changed | |
jQuery('.pm-dropdown select').on('change', function() { | |
var pmValueEmail = jQuery(this).val(); | |
var pmTextFullname = jQuery(this).find('option:selected').text(); | |
var pmFirstName = pmTextFullname.split(' ').slice(0, -1).join(' '); | |
// set the hidden text input field value | |
jQuery('.pm-first-name-text input').val(pmFirstName); | |
//console.log( 'Email:', pmValueEmail ); | |
//console.log( 'Full Name:', pmTextFullname ); | |
console.log( 'First Name:', pmFirstName ); | |
}); | |
// this does a "change" trigger right away (on load/render), to ensure the above "on change" runs even upon first loading (in case the dropdown was pre-populated via query string) | |
jQuery('.pm-dropdown select').trigger('change'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment