Check if textboxt value exists in Typeahead

I have this line of codes that will get the entire location in my database when a user types in text in my text box. The problem is that I want to check when the user changes the text in my text box and does not exist in my typeahead?

var path = "{{ route('search.location') }}"; $('input.typeahead').typeahead({ source: function (query, process) { return $.get(path, { query: query }, function (data) { return process(data); }); } }).blur(function () { if(source.indexOf($(this).val()) === -1) alert('Not exists'); }); 

I did some research using the .blur function, but I can't get it to work?

 ReferenceError: source is not defined 
+5
source share
1 answer

This is an example with local data. A boolean variable is something true if the query matches at least one element.

 $( document ).ready(function() { var data = [ {value: "Alabama"}, {value: "Delaware"}, {value: "Maine"} ]; var somethingFound = false; $("#the-basics .typeahead").typeahead( { minLength: 1, highlight: true }, { source: function(query, syncResults, asyncResults) { // use query to filter data var filteredData = data.filter(function(e){ return e.value.toLowerCase().startsWith(query.toLowerCase()); }); console.log(filteredData.length); somethingFound = filteredData.length > 0; // return filtered data syncResults(filteredData); } }).blur(function(){ if (!somethingFound) { alert('nothing found'); } }); }); 

see also at Plunker

+7
source

Source: https://habr.com/ru/post/1265722/


All Articles