I am working on a asp.net mvc project which will show the related search results ahead typing below the search box. I’ve added the twitter.typeahead.js with bower package manager in the project. Here is my code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="~/lib/typeahead.js/dist/typeahead.bundle.js"></script>
<title></title>
</head>
<body>
<div id="bloodhound">
<input class="typeahead" type="text" placeholder="States of USA">
</div>
</body>
</html>
<script type="text/javascript">
$(document).ready(function () {
var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];
var states = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
// `states` is an array of state names defined in "The Basics"
local: states
});
$('#bloodhound .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
source: states
});
});
</script>
Now it’s giving this error
Use of getPreventDefault() is deprecated.Use defaultPrevented instead.
TypeError: $(…).typeahead is not a function. jQuery.Deferred exception: $(…).typeahead is not a function @http://localhost:54331/Home/Search:85:9
j@https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js:2:29997
g/https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js:2:30313
undefined
bower.json
{
"name": "typeahead.js",
"version": "0.11.1",
"main": "dist/typeahead.bundle.js",
"dependencies": {
"jquery": ">=1.7"
},
"devDependencies": {
"jquery": "~1.7",
"jasmine-ajax": "~1.3.1",
"jasmine-jquery": "~1.5.2"
},
"homepage": "https://github.com/twitter/typeahead.js",
"_release": "0.11.1",
"_resolution": {
"type": "version",
"tag": "v0.11.1",
"commit": "87de059a7820b1e223f1c704fa12a624dbce3a4f"
},
"_source": "https://github.com/twitter/typeahead.js.git",
"_target": "v0.11.1",
"_originalSource": "typeahead.js",
"_direct": true
}
2
Answers
Lets get this working 😛
DEMO: https://jsfiddle.net/ipsjolly/6ydvth9q/1/
TypeAhead version 1.1.1
URL: https://cdnjs.cloudflare.com/ajax/libs/corejs-typeahead/1.1.1/typeahead.jquery.min.js
jQuery is latest
Source: http://twitter.github.io/typeahead.js/examples/
TypeScript code is without Bloodhound I dont know why this is for 😀
HTML
JS
Just to clarify, Bloodhound is not the issue.
Bloodhoud is the typeahead.js suggestion engine
There is a jsfiddle added on this stack overflow answer, that works perfectly with bloodhound.
I have also created one jsfiddle with the updated version typhead bloodhound and jquery version
You could try this version for your project