Twitter Typeahead Press Enter Key Go to Search Page
When i was working with Twitter typeahead auto-complete on my laravel 5 application, But when i press enter key, it's not redirect on my result page. I want to redirect on my result page that way i can display all records of matching search text.
I did try to solve many way but i can't solve simply. At last i found way using jquery "on keypress" event. You can see bellow code how it is work. So let's see bellow example:
In this example when you press enter on search input box. it will redirect on search page like bellow path:
http://test.hd/search?q=Ala
Example:
<!DOCTYPE html>
<html>
<head>
<title>Twitter typeahead press enter key go to search page</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/corejs-typeahead/0.11.1/typeahead.jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/corejs-typeahead/0.11.1/typeahead.bundle.min.js"></script>
</head>
<body>
<input type="text" class="typeahead" />
<script type="text/javascript">
var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California','Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii'];
var states = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: states
});
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
source: states
}).on('keypress', function(e) {
if (e.which == 13) {
var q = $('input.typeahead.tt-input').val();
window.location.href = "/search?q="+q;
}
});
</script>
</body>
</html>
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- JQuery Moment Subtract Days to Date Example
- Jquery Convert Object to String Example
- How to Change Column Name and Data Type in Laravel Migration?
- JQuery - How to Count Child Elements in Div?
- How to Check If Checkbox is Checked or Not in JQuery?
- How to Allow Only 10 Numbers in a Textbox using JQuery?
- How to Remove Key Value from Array using JQuery?
- How to Get Selected Radio Button Value in JQuery?
- PHP MySQL DataTables Server-side Processing Example
- Bootstrap Multiple Select with Checkboxes using Bootstrap-multiselect.js JS
- JQuery Select Box with Search using Select2 JS Example
- JQuery Tooltip Example using JQuery UI Plugin
- JQuery Notification Popup Box using Toastr JS Example
- Bootstrap Dynamic Autocomplete search using Typeahead JS