Skip to content

Quick Autocomplete



<!DOCTYPE html>
<html lang="en">
    <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="" >
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <link rel="stylesheet" href="">
    <link rel="stylesheet" href="./css/style.css">
    <title>Quick Autocomplete</title>
    <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
                <div class="form-group">
                    <input type="text" id="search" class="form-control form-control-lg"
                    placeholder="Enter state name or abbreviation" autocomplete="off"/>
                <div id="match-list"></div>
    <script src="./js/main.js"></script>


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

const searchStates = async searchText => {
    const res = await fetch("");
    const states = await res.json();

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

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


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

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

Leave a Reply

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

You are commenting using your 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: