Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save saifsultanc/1469a3be063dc773b2928af879027491 to your computer and use it in GitHub Desktop.

Select an option

Save saifsultanc/1469a3be063dc773b2928af879027491 to your computer and use it in GitHub Desktop.
gpapf-reset-phone-value-on-country-change.js
jQuery(document).ready(function($) {
const PHONE_FIELD_ID = 2; // Phone Field ID
const $phoneInput = $(`#input_${GFFORMID}_${PHONE_FIELD_ID}_raw`);
const $countrySelector = $('.iti__selected-country-primary');
if (!$phoneInput.length || !$countrySelector.length) {
return;
}
let previousCountry = getCurrentCountry();
function getCurrentCountry() {
return $countrySelector.text().trim();
}
function clearPhoneField() {
if ($phoneInput.length) {
$phoneInput.val('');
}
}
function handleCountryChange() {
const currentCountry = getCurrentCountry();
if (currentCountry && currentCountry !== previousCountry) {
previousCountry = currentCountry;
clearPhoneField();
}
}
function setupEvents() {
$countrySelector.on('click keydown', function(e) {
if (!e.key || e.key === 'Enter' || e.key === ' ' || e.keyCode === 13 || e.keyCode === 32) {
setTimeout(handleCountryChange, 200); // Small delay to allow UI update
}
});
$countrySelector.on('blur', function() {
setTimeout(handleCountryChange, 200);
});
}
setupEvents();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment