Login Register
Code2night
  • Home
  • Guest Posts
  • Blog Archive
  • Tutorial
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  1. Home
  2. Blogpost

Jquery Autocomplete

Date- Sep 06,2021

8368

Free Download Pay & Download
Jquery Autocomplete Autocomplete

So, starting from the beginning we will need few libraries for using Jquery Autocomplete . Basically, that is included in Jquery UI js. So we need to add these scripts in our webpage


<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

So, after adding these scripts in the head section of your webpage. We have to add this code in our webpage

 <script>
        

        $(document).ready(function () {

            var datalist = ["hindi", "english", "math", "science", "geography", "socialscience", "sanskrit"]
            $("#TestAutocomplete").autocomplete({
                source: datalist,

                response: function (event, ui) {
                    ui.content.push({
                        value: '', label: "Add new item"
                    });
                    ui.content.push({
                        value: event.target.value, label: event.target.value
                    });
                },


            }).data("ui-autocomplete")._renderItem = function (ul, item) {
                if (item.value == '') {
                    return $('<li class="ui-state-disabled" style="padding-left:5px;">' + item.label + '</li>').appendTo(ul);
                }
                var expression = new RegExp(this.term, 'gi');
                var highlight = item.label.replace(expression, "<span style='font-weight:bold;'>" + this.term + "</span>")
                return $("<li>")
                    .append("<a>" + highlight + "</a>")
                    .appendTo(ul);


            }
        })



    </script>

So, this function will be placed in document ready which will initialize the autocomplete. You will notice that we have create a array variable there. Which contains all the items of the Autocomplete.

.data("ui-autocomplete")._renderItem = function (ul, item) {
                if (item.value == '') {
                    return $('<li class="ui-state-disabled" style="padding-left:5px;">' + item.label + '</li>').appendTo(ul);
                }
                var expression = new RegExp(this.term, 'gi');
                var highlight = item.label.replace(expression, "<span style='font-weight:bold;'>" + this.term + "</span>")
                return $("<li>")
                    .append("<a>" + highlight + "</a>")
                    .appendTo(ul);


            }

This method is fired whenever Jquery Autocomplete tries to create html for rendering. Here we have adding bold wherever searche text is found.

@{
    ViewData["Title"] = "Autocomplete";
}
<style>

    .ui-state-active,
    .ui-widget-content .ui-state-active,
    .ui-widget-header .ui-state-active,
    a.ui-button:active,
    .ui-button:active,
    .ui-button.ui-state-active:hover {
        background: transparent !important;
        font-weight: normal;
        color: black !important;
        width: 100% !important;
        min-width: 200px !important;
        border:0px!important;
    }
</style>

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>


<div style="margin-top:50px;">
    <label for="TestAutocomplete">Subjects</label>
    <input type="text" id="TestAutocomplete" class="validate" />


</div>
@section scripts{
    <script>
        

        $(document).ready(function () {

            var datalist = ["hindi", "english", "math", "science", "geography", "socialscience", "sanskrit"]
            $("#TestAutocomplete").autocomplete({
                source: datalist,

                response: function (event, ui) {
                    ui.content.push({
                        value: '', label: "Add new item"
                    });
                    ui.content.push({
                        value: event.target.value, label: event.target.value
                    });
                },


            }).data("ui-autocomplete")._renderItem = function (ul, item) {
                if (item.value == '') {
                    return $('<li class="ui-state-disabled" style="padding-left:5px;">' + item.label + '</li>').appendTo(ul);
                }
                var expression = new RegExp(this.term, 'gi');
                var highlight = item.label.replace(expression, "<span style='font-weight:bold;'>" + this.term + "</span>")
                return $("<li>")
                    .append("<a>" + highlight + "</a>")
                    .appendTo(ul);


            }
        })



    </script>
}



So , this is the complete code available for Jquery Autocomplete. You can also download the source code by downloading from the attachment button. Or you can have a look at our youtube video for more details.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Linkedin Sign In using LinkedinLogin Nuget package in Asp-Net MVC
Apr 14, 2023
How to export view as pdf in Asp.Net Core
Jul 05, 2022
Linkedin Sign In using LinkedinLogin Nuget package in Asp-Net MVC
Jul 05, 2022
Convert HTML String To Image In C#
Jul 02, 2022

Comments

Contents

More in JavaScript

  • Slick Slider 14811 views
  • Card Number Formatting using jquery 11541 views
  • Alphanumeric validation in JavaScript 8758 views
  • Input Mask in Jquery 7437 views
  • Screen Recording with Audio using JavaScript in ASP.NET MVC 6567 views
View all JavaScript posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page