var getMenuItems = (function () { var cache = [], cacheMenuItems = function () { $('#nav li a').each(function () { var $this = $(this); cache.push({ value: $this.attr('href'), title: $this.text() }); }); return cache; }; return function (request, response) { var data = (cache.length) ? cache : cacheMenuItems(), matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), 'i'), filtered = $.grep(data, function (item) { return matcher.test(item.title); }); response(filtered); return true; }; })(); var renderLangItem = function (ul, item) { return $(document.createElement('li')) .data('item.autocomplete', item) .append( $(document.createElement('a')) .append($(document.createElement('img')).prop('src', item.icon)) .append(document.createTextNode(item.title)) ) .appendTo(ul); };