Javascript autocomplete

javascript autocomplete

Rate this article

Share this article

Autocomplete widget provides suggestions while you start typing into the input field.

Javascript autocomplete textbox control helps users by displaying a list of suggestions to the user so that while typing in the input field, suggestions for the typed query are searched and displayed.

Data binding, highlighting, typo correction, contextual suggestions are some of the visible features of autocomplete features.

Other famous javascript libraries like Jquery, AngularJs, ReactJs can be used. Here we will discuss how to implement pure javascript autocomplete easily.

  • Create HTML Page and add below code
<div class="center">
	<h2>Javascript Autocomplete</h2>
	<div class="autocomplete" style="width:88%; padding-right: 10px; float: left;">
		<input id="myInput" type="text" name="myCountry" placeholder="Enter Country name">
		<input type="submit">
  • Include CSS styles
<!-- -->
<!--In header section, we can include downloaded css-->
   <link rel="stylesheet" href="/styles.css">
  • Include Javascript
<!-- -->
<!-- In headre section, we can include downloaded js -->
    <script src="/js.js"></script>
  • Include data Source
<!-- -->
<!-- In the header section, we can include downloaded data.js as -->
    <script src="/data.js"></script>
  • Bind Autocomplete functionality to input box
<!-- add below script to end of the body tag-->
	/*An array containing all the country names in the world:*/
	var countries = [];
	/* cuntries_data is from data.js*/
	for(var i=0; i<countries_data.length; i++){
	/*initiate the autocomplete function on the "myInput" element, and pass along the countries array as possible autocomplete values:*/
	autocomplete(document.getElementById("myInput"), countries);

After this, you will be able to see the autocomplete by loading demo.html in the browser.


Javascript autocomplete


Add Search to your Website

Are you showing the right products, to the right shoppers, at the right time? Contact us to know more.
You may also like