Your code looks like you are providing options for typeahead.js, but you are not including that library. Instead, you are including the Bootstrap library, which has a typeahead feature. See this SO answer. Perhaps you are doing the reverse of what was going on there.
It sounds like you can use typeahead.js with Bootstrap; you just have to include Bootstrap first:
If you want to use this with a project like Bootstrap, all you have to do is include the JavaScript file for typeahead.js after Bootstrap’s JavaScript file and use our configuration options. [source]
It also sounds like the Bootstrap typeahead functionality was dropped altogether in Bootstrap 3 in favor of using typeahead.js:
Typeahead has been dropped, in favor of using Twitter Typeahead. [source]
You could include typeahead.js like this:
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://twitter.github.io/typeahead.js/releases/latest/typeahead.js"></script>
<script src="http://twitter.github.io/hogan.js/builds/2.0.0/hogan-2.0.0.js"></script>
<link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap-responsive.min.css">
Note: In addition to including the typeahead.js file, you will need to supply some CSS styles to get it to look nice. (For more information, read this.)
As for why your original code was doing nothing, you need to wrap your code in a document-ready handler. Otherwise it is executed as it is encountered, and at that time the #stations
selector does not match anything because the <input id="stations" />
element is not yet part of the DOM. Therefore the input element is never instrumented with the type-ahead functionality.
<script>
#(document).ready(function() {
$('#stations').typeahead(...);
});
</script>
This is not an issue with your second example because in that case the <script>
element comes after the <input>
element.
Finally, another reason your second example worked (while your first didn't) is because the "source" option is an option for the Bootstrap .typeahead()
function, not the typeahead.js .typeahead()
function. If you try that code after including the typeahead.js library, you will get the following error:
Error: one of local, prefetch, or remote is required