Javascript Search Box with Suggestions Dropdown

Category: Text & Input | October 12, 2019
Author: ddsky
Official Page: Go to Website
Last Update: December 15, 2019
License: MIT

The “uniBox.js” is a pure Javascript plugin that allows you to build a powerful search box with suggestions dropdown. It works without any dependencies.

It is s a stand-alone plugin that doesn’t need jQuery to make it work. But at the same time it also compatible with jQuery so if you want, you can make it work with jQuery.

Now you can build a full-featured search box that fetches the suggestions from external URLs. It helps you to improve the search functionality on your website.

It helps you to create an AJAX ready search form that quickly loads the suggestion as the user type something in the search input field.

Furthermore, this suggestion search also allows you to show an image thumbnail with each query in the dropdown.

With the help of its powerful configuration options, you can easily customize the plugin to make it work according to your project requirements.

Additionally, with the help of CSS, you can easily customize its layout according to your site template design.

How to Create Search Box with Suggestions Dropdown

To start building an Autocomplete Search Bar with Suggestions, you need to load its Javascript and CSS files in your HTML document. You can add them before closing the head tag or in the footer.

<!-- jQuery -->
<script src=""></script>

<!-- UniBox JS -->
<script src="js/unibox.min.js"></script>

<!-- UniBox CSS -->
<link rel="stylesheet" href="css/unibox.min.css"/>

After that, we will create a basic HTML form for the search box with a unique id. We simply have a form element defined with an input type text and a submit button.

The input type text field only needs to add a unique ID. You can also add a class to apply your own style.

   <input class="s searchbox" id="searchInput" type="text" placeholder="My search..." />
   <input class="btn-search" type="submit" value="search" />

The Javascript

The final step to initialize the plugin by adding the following script on your webpage. These options allow you to customize the functionality of the search bar.

var boxes = [];

sxQuery(document).ready(function() {

	var settings = {
		suggestUrl: '', // the URL that provides the data for the suggest -> this is just an example
		// suggestUrl: '', // the URL that provides the data for the suggest
		ivfImagePath: '', // the base path for instant visual feedback images

		instantVisualFeedback: 'all', // where the instant visual feedback should be shown, 'top', 'bottom', 'all', or 'none', default: 'all'
		throttleTime: 100, // the number of milliseconds before the suggest is triggered after finished input, default: 300ms
		extraHtml: undefined, // extra HTML code that is shown in each search suggest
		highlight: true, // whether matched words should be highlighted, default: true
		queryVisualizationHeadline: '', // A headline for the image visualization, default: empty
		animationSpeed: 200, // speed of the animations, default: 300ms
		callbacks: {
			enter: function(text,link){console.log('enter callback: '+text);}, // callback on what should happen when enter is pressed, default: undefined, meaning the link will be followed
			enterResult: function(text,link){console.log('enter callback result: ' + text);}, // callback on what should happen when enter is pressed on a result or a suggest is clicked
		placeholder: 'Search for something',
		minChars: 3, // minimum number of characters before the suggests shows, default: 3
		suggestOrder: [], // the order of the suggests
		suggestSelectionOrder: [], // the order of how they should be selected
		noSuggests: '<b>We haven\'t found anything for you, <u>sooorrryyy</u></b>',
		emptyQuerySuggests: {
							"Cool Things to Check Out":[
								{"id":"1","name":"Mason Jar Lid Lights","link":"","image":""},
								{"id":"2","name":"Spreadable Beer","link":"","image":""}
								{"name":"Canon PowerShot SX710 HS","image":"","id":"2179584","link":""},
		//maxWidth: 400 // the maximum width of the suggest box, default: as wide as the input box

	// apply the settings to an input that should get the unibox
	// apply to multiple boxes
	boxes = sxQuery(".s").unibox(settings);

That’s it. Let’s add this search bar to your website and enhance the search experience.

You Might Be Interested In:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.