Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
/ populate.js Public archive

Populate form fields from a JSON object.

License

Notifications You must be signed in to change notification settings

dannyvankooten/populate.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This repository moved to https://git.sr.ht/~dvko/populate.js on 2022-12-06 ⚠️

populate.js

A simple JavaScript populate( form, data ) function which will populate form fields from a JSON object.

Input types (text, email, select, multiple select, etc) are taken into account but not validated.

Usage

HTML

<form id="my-form">
	<input type="text" name="name" />
	<input type="email" name="email" />

	<input type="text" name="address[addr1]" />
	<input type="text" name="address[city]" />
	<input type="text" name="address[state]" />
</form>

JavaScript

// your JSON object
// keys have to match input names
var data = {
	"email": "[email protected]",
	"name": "John Doe",
	"address": {
		"addr1": "Street name",
		"city": "City name",
		"state": "State"
	}
}

// your containing element
var formElement = document.getElementById('my-form');

// populate the form with our JSON object
populate(formElement, data);

Install

Load the JavaScript file in your browser directly or install using NPM:

npm install populate.js

License

MIT licensed.