Unified interface for HTML form fields with two-way binding to object properties.
- A single getter/setter API for text inputs, checkboxes, radio buttons, and
<select>elements. - Two-way binding between a form field and an object property: assignments flow in either direction.
- Change-detection hooks via
onWrite(any value write) andonChange(user-driven change events). - Tiny browser build — html-form-field.min.js is under 4 KB minified and under 2 KB gzipped.
- First-class TypeScript types — see html-form-field.d.ts for the full surface.
import {formField} from "html-form-field"
interface Context {
nickname: string
email: string
favo: string
}
const form = document.querySelector("form")
const ctx = {} as Context
formField({form, bindTo: ctx, name: "nickname"})
console.log(ctx.nickname) // reads from the form field
ctx.nickname = "John" // writes back to the form field<form>
<ul>
<li>Nickname: <input type="text" name="nickname" value="Alice"></li>
<li>Email: <input type="email" name="email" value="alice@example.com"></li>
<li>Favorites:
<label><input type="checkbox" name="favo" value="tech">Tech</label>
<label><input type="checkbox" name="favo" value="travel">Travel</label>
<label><input type="checkbox" name="favo" value="trading">Trading</label>
</li>
</ul>
</form>const email = formField({form, name: "email"})
console.log(email.value) // read the current value
email.value = "john@example.com" // assign a new valueconst favo = formField({form, name: "favo", delim: ","})
favo.toggle("tech") // toggle one checkbox
favo.toggle("travel", true) // force-check
favo.toggle("trading", false) // force-uncheck
console.log(favo.has("travel")) // is this option currently selected?
// Shortcut for items().at(index)
const firstItem = favo.itemAt(0)
console.log(firstItem.checked)
// Shortcut for items().find(v => v.value === value)
const travelItem = favo.itemOf("travel")
console.log(travelItem.checked)formField({
form,
bindTo: ctx,
name: "email",
onWrite: ({name, value}) => sessionStorage.setItem(name, value),
onChange: ({name, value}) => submitForm(),
defaults: [sessionStorage.getItem("email")],
})Copyright (c) 2025-2026 Yusuke Kawasaki
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.