var destinationLoc = []; $(document).ready(function () { var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g'); if ($(".places").length > 0) { $.ajax({ crossDomain: true, url: "https://www.kenya-airways.com/locations.aspx", data: { call: "cities" }, dataType: "jsonp", error: function (error, a, b) { console.log(error, a, b); }, success: function (data) { destinationLoc = data ? data : destinationLoc; var regex; $.each($(".places"), function (index, elem) { var locFrom = index == 0; if ($(elem).length <= 0) return; $(elem).autocomplete({ minLength: 2, source: function (request, response) { regex = new RegExp('(' + request.term.replace(reEscape, '\\$1') + ')', 'gi'); var filterArr = $.grep(destinationLoc, function (item, i) { var label = item.locName + (item.locAirport ? " - " + item.locAirport : " ") + "(" + item.locCode + ")" + (item.locCountry ? ", " + item.locCountry : ""); return regex.test(label) && (item.locFrom && locFrom || item.locTo) }); elem.valide_data = filterArr; response($.map(filterArr, function (item, index) { var label = item.locName + (item.locAirport ? " - " + item.locAirport : " ") + "(" + item.locCode + ")" + (item.locCountry ? ", " + item.locCountry : ""); item.value = item.locCode; item.html = label.replace(regex, '$1<\/strong>'); return item; })); } }).data("ui-autocomplete")._renderItem = function (ul, item) { ul.css("z-index", "10000"); return $('