DEV Community

Cover image for Creating a Dynamic Address Autofill Form with HTML, JavaScript, and Datalist
HexShift
HexShift

Posted on

Creating a Dynamic Address Autofill Form with HTML, JavaScript, and Datalist

Autofill functionality enhances form usability by speeding up data entry and reducing user frustration. In this tutorial, we’ll create a form where users can select or auto-complete their address from a predefined list using native HTML elements and a bit of JavaScript.


Step 1 - Setup the Basic Form Structure

Use the `` element to provide address suggestions. Pair it with a regular input field to make autofill seamless.



    Address:











    City:




    State:



  Submit

Enter fullscreen mode Exit fullscreen mode

Step 2 - Parse the Selected Address

When an address is selected, we’ll split it into parts and autofill the city and state fields.

document.getElementById("addressInput").addEventListener("input", function () {
  const value = this.value;
  const parts = value.split(",");

  if (parts.length === 3) {
    const [street, city, state] = parts.map((part) => part.trim());
    document.getElementById("city").value = city;
    document.getElementById("state").value = state;
  } else {
    document.getElementById("city").value = "";
    document.getElementById("state").value = "";
  }
});
Enter fullscreen mode Exit fullscreen mode

Step 3 - Add Basic Styling

Style the form for better clarity and visual hierarchy.

form {
  max-width: 400px;
  margin: 20px auto;
  display: flex;
  flex-direction: column;
}

label {
  margin-bottom: 15px;
  font-weight: bold;
}

input {
  padding: 8px;
  width: 100%;
  box-sizing: border-box;
  margin-top: 5px;
}
Enter fullscreen mode Exit fullscreen mode

Use Case Scenario

This pattern is ideal for apps requiring repeated address entry, such as admin dashboards, e-commerce checkouts, or CRM tools. By pre-populating city and state fields, you reduce input errors and save time for the user without relying on external APIs or libraries.


✅ Pros and ❌ Cons

✅ Pros:

  • 🚀 Native HTML, no external dependencies
  • 🧠 Reduces user input and cognitive load
  • ✨ Enhances UX with minimal JS

❌ Cons:

  • 📋 Static list of suggestions may become outdated
  • ⛔ Not a replacement for true address validation
  • 🔍 Limited support for dynamic datasets

Summary

This tutorial showed how to create an address autofill form using `` and JavaScript. It’s a lightweight, accessible way to enhance form UX, particularly for repetitive input patterns.

For deeper dives into advanced form patterns, custom fields, validation strategies, and responsive design, grab my 19-page PDF guide:

Mastering Advanced HTML Forms: A Complete Guide to Building Dynamic, User-Friendly Forms – just $5.

Whether you're creating registration pages, checkout flows, or multi-step data tools, it’s your go-to resource for mastering forms in the modern web.


If this was helpful, you can also support me here: Buy Me a Coffee

JavaScript tools that help you ship faster

JavaScript tools that help you ship faster

Skip boilerplate and focus on features. Kinde handles auth, access control, and billing behind the scenes.

Get a free account

Top comments (0)

JavaScript-ready auth and billing that just works

JavaScript-ready auth and billing that just works

Stop building auth from scratch. Kinde handles authentication, user management, and billing so you can focus on what matters - shipping great products your users will love.

Get a free account

[Workshop] Building and Monitoring AI Agents and MCP servers

Building with AI is different. Sentry has built some tools to help. In this workshop, you’ll learn how to monitor your agents, debug your MCP servers and how to debug with Seer.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️