(function() { function initCompanyAutocomplete() { if (typeof jQuery === 'undefined') { console.error('jQuery is not loaded.'); return; }
jQuery(function($) { var ajaxUrl = 'https://www.hccinstitute.org/wp-admin/admin-ajax.php'; var $companyInputs = $('#inf_field_Company');
$companyInputs.each(function() { var $companyInput = $(this); var $dropdown = $('
').css({ position: 'absolute', background: '#fff', border: '1px solid #ccc', 'z-index': 1000, 'list-style-type': 'none', margin: 0, padding: 0, 'max-height': '200px', 'overflow-y': 'auto', display: 'none' }); $companyInput.parent('.infusion-field-account-full').css('position', 'relative'); $companyInput.parent('.infusion-field-account-full').append($dropdown); $companyInput.on('keyup', function() { var term = $companyInput.val();
if (term.length < 3) { $dropdown.hide(); return; } $dropdown.css({ top: $companyInput.outerHeight(), left: 0, width: $companyInput.outerWidth() }); // AJAX request $.ajax({ url: ajaxUrl, method: 'GET', dataType: 'json', data: { action: 'get_matching_companies', term: term }, success: function(data) { if (data.length > 0) { $dropdown.empty(); $.each(data, function(index, company) { var $item = $('
').text(company).css({ padding: '5px', cursor: 'pointer' }); $item.on('click', function() { $companyInput.val(company); $dropdown.hide(); });
$dropdown.append($item); }); $dropdown.show(); } else { $dropdown.hide(); } }, error: function(xhr, status, error) { console.log('Error fetching companies: ' + error); $dropdown.hide(); } }); });
// Hide the dropdown when clicking outside $(document).on('click', function(e) { if (!$(e.target).closest($companyInput).length && !$(e.target).closest($dropdown).length) { $dropdown.hide(); } }); }); }); }
// Initialize the autocomplete when DOM is ready if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initCompanyAutocomplete); } else { initCompanyAutocomplete(); }
// Retry initialization if jQuery isn't loaded yet (function waitForjQuery() { if (typeof jQuery === 'undefined') { setTimeout(waitForjQuery, 50); } else { initCompanyAutocomplete(); } })(); })();
