Skip to content
Advertisements

Quick Autocomplete

https://codepen.io/guldus/pen/wbybgO

Github: https://github.com/Furkan-Gulsen/HTML-CSS-JAVASCRIPT/tree/master/Quick%20Autocomplete


HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" >
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">
    <link rel="stylesheet" href="./css/style.css">
    
    
    <title>Quick Autocomplete</title>
</head>
<body>
    
    <div class="container mt-5">
        <div class="row">
            <div class="col-md-6 m-auto">
                <h3 class="text-center mb-3">
                    <i class="fas fa-flag-usa"></i>State Capital Lookp
                </h3>
                <div class="form-group">
                    <input type="text" id="search" class="form-control form-control-lg"
                    placeholder="Enter state name or abbreviation" autocomplete="off"/>
                </div>
                <div id="match-list"></div>
            </div>
        </div>
    </div>
    <script src="./js/main.js"></script>
</body>
</html>

JS

const search = document.getElementById("search");
const matchList = document.getElementById("match-list");

const searchStates = async searchText => {
    const res = await fetch("https://raw.githubusercontent.com/Furkan-Gulsen/HTML-CSS-JAVASCRIPT/master/Quick%20Autocomplete/data/states.json");
    const states = await res.json();

    let matches = states.filter(state => {
        const regex = new RegExp(`^${searchText}`,"gi");
        return state.name.match(regex) || state.abbr.match(regex);
    })

    if(searchText.length === 0){
        matches = [];
        matchList.innerHTML = "";
    }

    outputHtml(matches);
} 

const outputHtml = matches => {
    if(matches.length >0){
        const html = matches.map(match => 
            ` <div class="card card-body mb-1">
                <h4>${match.name} (${match.abbr})
                    <span class="text-prim">${match.capital}</span>
                    <small>Lat: ${match.lat} \ Long: ${match.long}</small>                    
                </h4>
            </div>`
            ).join("")
        matchList.innerHTML = html;
    }
}

search.addEventListener("input", () => searchStates(search.value));
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: