Created
July 9, 2025 13:45
-
-
Save saifsultanc/1469a3be063dc773b2928af879027491 to your computer and use it in GitHub Desktop.
gpapf-reset-phone-value-on-country-change.js
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
| 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