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

Top comments (0)

👋 Kindness is contagious

Dive into this informative piece, backed by our vibrant DEV Community

Whether you’re a novice or a pro, your perspective enriches our collective insight.

A simple “thank you” can lift someone’s spirits—share your gratitude in the comments!

On DEV, the power of shared knowledge paves a smoother path and tightens our community ties. Found value here? A quick thanks to the author makes a big impact.

Okay