$( function() { /*-------------------------- Call Autocomplete for search bar in homepage publication ------------------------------*/ var languageFolder = /^\/(en|fr|de){1}\//g.exec(window.location.pathname); var query = ""; if( languageFolder != null && ( languageFolder[1] != null || languageFolder[1] != undefined ) ){ query = "forceLanguage=" + languageFolder[1]; } //add the autocomplete html in the correct location var acAppendTo = ""; if (languageFolder != null && languageFolder != undefined){ var languagePage = languageFolder[1].toUpperCase(); }else{ var languagePage = "EN"; } $("html").find(".autocomplete-search").each(function () { if($(this).find(".input-group").length){ acAppendTo = ".autocomplete-search .input-group"; }else if($(this).parent().find(".autocomplete-search.search-form-responsive").length) { acAppendTo = ".autocomplete-search .input-search-reponsive"; }else{ acAppendTo = ".autocomplete-search form"; } if($(this).find(".autocomplete-search__input").length){ var keywordsSearch = getDataTypeUrl( "search", "keywords", "keywordsUrl" ); $(this).find(".autocomplete-search__input").each(function(){ if($(this).parent().find(".autocomplete-search__input.autocomplete-search__input--publication").length){ $(this).autocomplete({ minLength: 3, appendTo : acAppendTo, source: function( request, response ) { $.ajax( { url: keywordsSearch, dataType: "json", data: { website: "eib", language: languagePage, term: request.term, subTypes: "publications" }, success: function( data ) { var srchString = $('.autocomplete-search__input.autocomplete-search__input--publication').val(); response($.map(data.slice(0,5), function(item){ var itemA = item; item = item[0].toUpperCase() + item.slice(1); item = item.replace(new RegExp(srchString, "gi"), function (srchString) { return ""+srchString+""; }); return { value: itemA, label: item }; //Note to hassan : in order to have an html LABEL i override the methode renderItem for autocomplete in jquery from .text() to .html() !!!!! })); $(".autocomplete-search li.ui-menu-item:nth-of-type(1)").prepend("