ItSolutionStuff.com

Twitter Typeahead Press Enter Key Go to Search Page

By Hardik Savani • June 15, 2023
Javascript Bootstrap jQuery

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

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

JQuery Moment Subtract Days to Date Example

Read Now →

Jquery Convert Object to String Example

Read Now →

How to Change Column Name and Data Type in Laravel Migration?

Read Now →

JQuery - How to Count Child Elements in Div?

Read Now →

How to Check If Checkbox is Checked or Not in JQuery?

Read Now →

How to Allow Only 10 Numbers in a Textbox using JQuery?

Read Now →

How to Remove Key Value from Array using JQuery?

Read Now →

How to Get Selected Radio Button Value in JQuery?

Read Now →

PHP MySQL DataTables Server-side Processing Example

Read Now →

Bootstrap Multiple Select with Checkboxes using Bootstrap-multiselect.js JS

Read Now →

JQuery Select Box with Search using Select2 JS Example

Read Now →

JQuery Tooltip Example using JQuery UI Plugin

Read Now →

JQuery Notification Popup Box using Toastr JS Example

Read Now →

Bootstrap Dynamic Autocomplete search using Typeahead JS

Read Now →