Form Builder in JavaScript
Create labelled fields in JavaScript with DOM methods, then preview FormData as JSON. Unique names keep fields separate; even number inputs return strings.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<label>Field label <input id="label" value="Student name"></label><select id="type" aria-label="Field type"><option value="text">Text</option><option value="number">Number</option></select><button id="add" type="button">Add field</button><form id="preview"><div id="fields"></div><button>Submit preview</button></form><pre id="result"></pre>7<script>8let nextId = 1;9const fields = document.querySelector('#fields');10document.querySelector('#add').onclick = () => {11 const text = document.querySelector('#label').value.trim();12 if (!text) return;13 const row = document.createElement('p');14 const label = document.createElement('label'); label.textContent = text + ' ';15 const input = document.createElement('input'); input.name = 'field_' + nextId++;16 input.type = document.querySelector('#type').value; input.required = true;17 label.append(input);18 const remove = document.createElement('button'); remove.type = 'button'; remove.textContent = 'Remove ' + text;19 remove.onclick = () => row.remove();20 row.append(label, remove); fields.append(row);21};22document.querySelector('#preview').onsubmit = event => {23 event.preventDefault();24 const values = Object.fromEntries(new FormData(event.currentTarget));25 document.querySelector('#result').textContent = JSON.stringify(values, null, 2);26};27</script>28</body>29</html>
Sample Output
"field_1": "Meera"
The generated input name becomes the object key, and FormData supplies its string value.
In this one
- Create labelled controls dynamically
- Give each field a unique name
- Remove a generated field
- Collect submitted values with FormData
How it works
- 1
Trim the requested field label.
- 2
Create a row containing a label, a named input, and a non-submit Remove button.
- 3
Append the row to the preview form.
- 4
On preview submit, collect FormData without reloading.
- 5
For one field containing Meera, display field_1 mapped to the string Meera.
Edge cases
Duplicate visible labels
Two fields may both be called Name.
Their generated field_ IDs still differ, so Object.fromEntries does not overwrite one answer with the other.
Button submits accidentally
Buttons inside a form default to submit.
The generated Remove button explicitly uses type button so deleting a field does not submit the form.
What's going on
A form builder creates input fields from choices made on the page. A club can use the idea to prepare a simple registration form. This example adds labelled text or number fields, removes them, and previews submitted values.
The Add field button creates new page elements rather than inserting an HTML string. Each input receives a unique name such as field_1. A name is the key used when the form collects its values. The visible label can be changed independently and may contain spaces without breaking that key.
Submitting the preview reads FormData, which collects successful named form controls. Object.fromEntries turns those name-value pairs into an object, and JSON.stringify formats the result. Input values are still strings, including number-field values. The generated fields are required, but there is no server save or database in this lesson. A real builder should store the form design separately from the answers and validate answers again on its server.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What identifies a value in FormData?
Pick an answer to check it.
Question 2
Why is Remove type button?
Pick an answer to check it.
Questions people ask
Why is a numeric answer still in quotes?
FormData returns ordinary input values as strings. Convert and validate specific numeric fields when processing them.
Can I make the same label twice?
Yes. Unique input names keep the answers separate even when the visible labels match.
Does this save the form design?
No. It only changes the current page and displays a submission preview.