javascript autocomplete textbox example

JavaScript Autocomplete Textbox Tutorial

Autocomplete allows the browser to predict the value. It enables users to quickly find and select from a pre-populated list of values as they type, leveraging searching and filtering. In this asp.net tutorial, we will discuss javascript autocomplete textbox.

Note: The autocomplete attribute works with the following <input> types: text, search, url, tel, email, password, datepickers, range, and color.

Create Autocomplete Textbox JavaScript

Find the code snippet below:-

<!DOCTYPE html>
<html>
<head>
    <title>javascript autocomplete textbox in asp.net</title>
    <link href="css.css" rel="stylesheet" />
</head>

<body>
    <!--BEGIN SEARCH-CONTAINER -->
    <div class="search-container">
        <div class="search-header">
            <h2>Autocomplete textbox in javascript demo</h2>
        </div><!-- end div .contact-header-->
        <!--BEGIN MAIN-CONTAINER -->
        <div class="main-container">
            <form class="auto-suggest-form">
                <fieldset>
                    <div class="input-field left-align">
                        <input type="text" id="auto-search" placeholder="Search Here" />
                    </div><!-- end div .input-field-->
                    <div class="input-field right-align">
                        <select id="searchType">
                            <option value="title">Title</option>
                            <option value="description">Description</option>
                            <option value="price">Price</option>
                        </select>
                    </div><!-- end div .input-field-->
                </fieldset>
            </form>

            <div class="data-container">
                <ul class="data-list">
                    <li title-val="Awsome Camera" description-val="Awsome Camera is available with free two betteries." price-val="100.70">
                        <div class="list-img">
                            <img src="photo1.png" width="50" height="50" />
                        </div>
                        <div class="list-details">
                            <a href="#">Awsome Camera</a>
                            <p>Awsome Camera is available with free two betteries.</p>
                            <span>$100.70<</span>>
                        </div>
                    </li>
                    <li title-val="For Photography" description-val="Canon camera 12 mega pixel is best for photography." price-val="765">
                        <div class="list-img">
                            <img src="photo1.png" width="50" height="50" />
                        </div>
                        <div class="list-details">
                            <a href="#">For Photography</a>
                            <p>Canon camera 12 mega pixel is best for photography.</p>
                            <span>$765<</span>>
                        </div>
                    </li>
                    <li title-val="Cheap DSLR" description-val="Here is offer for cheap camera, tomorrow will be last date of this offer." price-val="108.01">
                        <div class="list-img">
                            <img src="photo1.png" width="50" height="50" />
                        </div>
                        <div class="list-details">
                            <a href="#">Cheap DSLR</a>
                            <p>Here is offer for cheap camera, tomorrow will be last date of this offer.</p>
                            <span>$108.01<</span>>
                        </div>
                    </li>
                    <li title-val="New Model Canon" description-val="New Model Canon will be launch in november 2016." price-val="3020.00">
                        <div class="list-img">
                            <img src="dslr.png" width="50" height="50" />
                        </div>
                        <div class="list-details">
                            <a href="#">New Model Canon</a>
                            <p>New Model Canon will be launch in november 2016.</p>
                            <span>$3020.00<</span>>
                        </div>
                    </li>
                    <li title-val="Used DSLR" description-val="I want to sell my two years old camera." price-val="76.08">
                        <div class="list-img">
                            <img src="dslr.png" width="50" height="50" />
                        </div>
                        <div class="list-details">
                            <a href="#">Used DSLR</a>
                            <p>I want to sell my two years old camera.</p>
                            <span>$76.08<</span>>
                        </div>
                    </li>
                    <li title-val="Special Offer For Camera" description-val="We are offering a special discount in october 2016." price-val="340.60">
                        <div class="list-img">
                            <img src="photo1.png" width="50" height="50" />
                        </div>
                        <div class="list-details">
                            <a href="#">Special Offer For Camera</a>
                            <p>We are offering a special discount in october 2016.</p>
                            <span>$340.60<</span>>
                        </div>
                    </li>
                    <li title-val="Unique Feature Camera" description-val="I want to sell my customized camera with seven lens and other gadgets." price-val="718.54">
                        <div class="list-img">
                            <img src="photo1.png" width="50" height="50" />
                        </div>
                        <div class="list-details">
                            <a href="#">Unique Feature Camera</a>
                            <p>I want to sell my customized camera with seven lens and other gadgets.</p>
                            <span>$718.54<</span>>
                        </div>
                    </li>
                    <li title-val="Amazing Result" description-val="Amazing Result camera will be available after 1st November." price-val="245">
                        <div class="list-img">
                            <img src="dslr.png" width="50" height="50" />
                        </div>
                        <div class="list-details">
                            <a href="#">Amazing Result</a>
                            <p>Amazing Result camera will be available after 1st November.</p>
                            <span>$245<</span>>
                        </div>
                    </li>
                    <li title-val="Urgent Sell" description-val="We wanted to urgent sell two hundreds DSLR cameras." price-val="633">
                        <div class="list-img">
                            <img src="dslr.png" width="50" height="50" />
                        </div>
                        <div class="list-details">
                            <a href="#">Urgent Sell</a>
                            <p>We wanted to urgent sell two hundreds DSLR cameras.</p>
                            <span>$633<</span>>
                        </div>
                    </li>
                </ul><!-- end div .data-list-->
            </div><!-- end div .data-container-->
        </div>
        <!--END MAIN-CONTAINER-->
    </div>
    <!--END SEARCH-CONTAINER -->

    <script src="AutoSearch.js"></script>
</body>
</html>

AutoSearch.js
var s = 'auto-search';
var c = 'title-val';
var d = 'description-val';
var p = 'price-val';
var fl = false;
var dt = [];
document.getElementById(s).onkeyup = function() {
    var searchType = document.getElementById("searchType");
    selectedSearch = searchType.options[searchType.selectedIndex].value;
    var searchVal = this.value.toLowerCase();
    var itemName = '';
    if (selectedSearch === 'title') {
        var filterItems = document.querySelectorAll('[' + c + ']');
        itemName = c;
    }
    if (selectedSearch === 'description') {
        var filterItems = document.querySelectorAll('[' + d + ']');
        itemName = d;
    }
    if (selectedSearch === 'price') {
        var filterItems = document.querySelectorAll('[' + p + ']');
        itemName = p;
    }
    for (var i = 0; i < filterItems.length; i++) {
        if (!fl) {
            dt.push(dataShow(filterItems[i]));
        }
        filterItems[i].style.display = 'none';
        if (filterItems[i].getAttribute(itemName).toLowerCase().indexOf(searchVal) >= 0) {
            filterItems[i].style.display = dt[i];
        }
    }
    fl = true;
}

function dataShow(element) {
    var elementCss = element.currentStyle || window.getComputedStyle(element, "");
    return elementCss.display;
}

Style.css
@charset "utf-8";
/* CSS Document */
/* === HTML Section === */
body {
    font-family: Helvetica,Arial,sans-serif;
    font-size: 14px;
    color: #58666e;
    background: #F5F5F5;
}

.search-container {
    width: 835px;
    margin: 50px auto;
    padding: 35px;
    border: 1px solid #cbcbcb;
    background-color: #FFF;
}

.search-header {
    width: auto;
    height: 50px;
    background-color: #F5F5F5;
    border: 1px solid #cbcbcb;
    border-bottom: none;
    padding-left: 22px;
}

    .search-header h2 {
        font-weight: 500;
    }

.main-container {
    padding: 10px;
    border: 1px solid #cbcbcb;
    position: relative;
}

.data-container {
    min-height: 200px;
    max-height: 300px;
    overflow: scroll;
}
/* === List Section === */
ul.data-list {
    list-style: none;
}

    ul.data-list li {
        width: 92%;
        height: auto;
        float: left;
        padding: 13px;
        border: 1px solid #cbcbcb;
    }

        ul.data-list li:hover {
            background-color: #F5F5F5;
        }

.list-img, .list-details {
    float: left;
}

.list-img {
    width: 11%;
}

.list-details {
    width: 80%;
    line-height: 5px;
}

    .list-details a {
        font-weight: bold;
        color: #58666e;
        text-decoration: none;
    }
/* === Form Section === */
form.auto-suggest-form .input-field {
    width: 49%;
}

form.auto-suggest-form .right-align {
    float: right;
}

form.auto-suggest-form .left-align {
    float: left;
}

form.auto-suggest-form fieldset {
    border: none;
}

form.auto-suggest-form input {
    width: 96.2%;
    padding: 5px 4px;
}

form.auto-suggest-form *:focus {
    outline: none;
}

form.auto-suggest-form textarea {
    width: 98.4%;
    padding: 5px 4px;
}

form.auto-suggest-form select {
    width: 99.5%;
    padding: 5px 2px;
    color: #58666e;
}

Autocomplete Textbox in Javascript Demo | Download Source Code

Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.

LEAVE A REPLY

Please enter your comment!
Please enter your name here