jQuery(function ($) { 'use strict'; var FORM_ID = 19; // Radio choice ID -> zipcode input selector var TARGETS = { choice_19_27_0: '#input_19_9', // Care for a loved one choice_19_27_1: '#input_19_28' // Caregiver opportunities }; // Conditional logic shows/enables the field right after the change event, // so retry briefly until the field is visible and enabled. function focusWhenReady(selector, attemptsLeft) { var $el = $(selector); if ($el.length && $el.is(':visible') && !$el.prop('disabled')) { $el.trigger('focus'); return; } if (attemptsLeft > 0) { setTimeout(function () { focusWhenReady(selector, attemptsLeft - 1); }, 50); } } // Delegated on document so it survives AJAX re-renders and validation reloads $(document).on('change', '#gform_' + FORM_ID + ' input[name="input_27"]', function () { var selector = TARGETS[this.id]; if (!selector) return; focusWhenReady(selector, 20); // up to ~1 second of retries }); });