function autocomplete() { var apiurl = $(this).data("apiurl"); var search = $(this).val(); var elem = $(this); if (search.length > $(this).data("strlen")) { $.post({ url: apiurl, data: { q: search }, success: function (data) { createAutocompleteDropdown(elem, JSON.parse(data)); } }); } } function createAutocompleteDropdown(elem, values) { var dropdownmenue = $('[aria-labelledby=' + elem.attr("id") + ']'); var dropdownItems = dropdownmenue.find(".dropdown-item"); dropdownItems.remove(); for (index = 0; index < values.length; index++) { var value = values[index]; var regex = new RegExp((elem).val(), "ig"); var displayvalue = value.replace(regex, ("" + (elem).val() + "")); var button = $(""); button.click(dropdownButtonClick); dropdownmenue.append(button); } } function dropdownButtonClick() { var string = $(this).data("val"); $('#' + $(this).attr("aria-textfield")).val(string); }