XS 44
').appendTo($(this)).hide(); // Listening to keyup and change on the text field within these search forms. input.attr('autocomplete', 'off').bind('keyup change', function() { // What's the search term? var term = $(this).val(); // What's the search form? var form = $(this).closest('form'); // What's the search URL? var searchURL = '/search?type=product&q=' + term; // What's the search results list? var resultsList = form.find('.search-results'); // If that's a new term and it contains at least 3 characters. if (term.length > 3 && term != $(this).attr('data-old-term')) { // Saving old query. $(this).attr('data-old-term', term); // Killing any Ajax request that's currently being processed. if (currentAjaxRequest != null) currentAjaxRequest.abort(); // Pulling results. currentAjaxRequest = $.getJSON(searchURL + '&view=json', function(data) { // Reset results. resultsList.empty(); // If we have no results. if(data.results_count == 0) { // resultsList.html('
  • No results.
  • '); // resultsList.fadeIn(200); resultsList.hide(); } else { // If we have results. $.each(data.results, function(index, item) { var price = item.price; if ( item.comparePrice ){ price = '' + item.comparePrice + '' + item.price; } if(item.title==='Lucky Dip') price += ' - £25.00'; if(!item.available) price = 'Sold out'; var link = $('').attr('href', item.url); link.append(''); link.append('' + item.title + ''); link.append('' + price + ''); link.wrap('
  • '); resultsList.append(link.parent()); }); // The Ajax request will return at the most 10 results. // If there are more than 10, let's link to the search results page. if(data.results_count > 20) { resultsList.append('
  • See all results (' + data.results_count + ')
  • '); } resultsList.fadeIn(200); } // $(".search-results li").equalizeHeight(); }); } }); }); // Clicking outside makes the results disappear. // $('body').bind('click', function(){ // $('.search-results').hide(); // }); });