Use the labeled fields and Add/Remove text buttons. Without JavaScript or after a module-loading failure, authored initial fields retain their labels and values. Explicit templates are inert until enhancement; use initial groups when a native fallback is needed.
Start with a labeled field
Wrap the first group in
<form-repeatable>. Add Another copies it
and numbers the new label and field ID. Without JavaScript,
the initial field stays editable.
Travel Stops
Add stops to your journey, then remove one. Remove buttons name the stop they delete. Keyboard users can activate either action with Enter or Space; focus stays on an available action after the groups change.
Code
<form-repeatable>
<div>
<label for="stop-1">Stop 1</label>
<input id="stop-1" type="text"
name="stops[]"
placeholder="Enter location">
</div>
</form-repeatable>
Live example
Keep initial values
Author several groups when you already have values to edit. Their labels and values remain available before enhancement and if the module fails to load.
Contact Information
These three phone fields start with saved values. New
groups start from the first field’s authored default,
not its current edits. Numbering also changes digits in
value attributes. Use an explicit template
if new fields should start blank, and see the
numbering reference
before using numbered defaults.
Code
<form-repeatable>
<div>
<label for="phone-1">Phone 1</label>
<input id="phone-1" type="tel"
name="phones[]"
value="555-0100">
</div>
<div>
<label for="phone-2">Phone 2</label>
<input id="phone-2" type="tel"
name="phones[]"
value="555-0101">
</div>
<div>
<label for="phone-3">Phone 3</label>
<input id="phone-3" type="tel"
name="phones[]"
value="555-0102">
</div>
</form-repeatable>
Live example
Choose a template for new groups
Use <template> to define new groups with
{n} placeholders in labels and attributes. You
can also supply initial groups alongside it for a native
fallback with different defaults.
Email Addresses
This template-only example creates one email field after enhancement. Without JavaScript, the template stays inert: it provides no editable email field.
Code
<form-repeatable>
<template>
<div>
<label for="email-{n}">Email {n}</label>
<input id="email-{n}"
type="email"
name="emails[]"
placeholder="user@example.com">
</div>
</template>
</form-repeatable>
Live example
Limit the number of groups
Control the number of allowed groups with
min and max attributes.
Team Members (2-5 allowed)
Author two initial fields: min="2" prevents
removal below two but doesn’t create missing fields.
Remove buttons disappear at the minimum, and Add
disappears at five. See the
attribute reference
for invalid values.
Code
<form-repeatable min="2" max="5">
<div>
<label for="member-1">Member 1</label>
<input id="member-1" type="text"
name="members[]" placeholder="Team member name">
</div>
<div>
<label for="member-2">Member 2</label>
<input id="member-2" type="text"
name="members[]" placeholder="Team member name">
</div>
</form-repeatable>
Live example
Submit repeatable and ordinary fields
Named fields in the groups contribute values alongside
ordinary form fields. This demo displays
FormData locally; it doesn’t send a
registration. Your application must validate group values: a
group’s required field doesn’t block
parent-form submission. See the
form reference
for validation, reset, and disabled-state limits.
Event Registration Form
Fill in the event details and add attendees. Register Event displays the current values. Reset form clears these text fields but keeps the current groups; it doesn’t bring back removed attendees.
Code
<form id="combined-form">
<label for="event-name">Event Name</label>
<input id="event-name" name="eventName"
type="text" placeholder="Annual Conference" required>
<fieldset>
<legend>Attendees (Min: 1, Max: 10)</legend>
<form-repeatable min="1" max="10">
<div>
<label for="attendee-1">Attendee 1</label>
<input id="attendee-1" type="text"
name="attendees[]"
placeholder="Full name" required>
<label for="att-email-1">Email 1</label>
<input id="att-email-1" type="email"
name="emails[]"
placeholder="email@example.com">
</div>
</form-repeatable>
</fieldset>
</form>
Live example
What the output contains
When you submit the form, notice how the FormData includes:
-
Ordinary fields
(
eventName,eventDate,eventLocation) -
Repeatable fields
(
attendees[],emails[]) - Repeated names grouped into arrays for this JSON display; the form data itself keeps repeated entries
Without JavaScript, the initial attendee fields still belong to the form. The local JSON preview and additional groups require JavaScript.
Add and remove a group from your application
The application buttons below call
addGroup() and removeGroup(id).
Keep the returned stable ID rather than an index, which
changes after removals. The count comes from read-only
groupCount. Try adding beyond three to see the
blocked result; no extra group or event is created.
Code
<form-repeatable id="command-repeatable" max="3">
<div>
<label for="api-contact-1">Contact 1</label>
<input id="api-contact-1" name="api-contacts[]">
</div>
</form-repeatable>
<script type="module">
import '../define.js';
const repeatable = document.querySelector('#command-repeatable');
// These are the calls the application buttons make:
const added = repeatable.addGroup();
if (added) {
repeatable.removeGroup(added.id);
}
console.log(repeatable.groupCount);
</script>
Live example
The initial field stays editable without JavaScript; application actions require the module.
Events
Listen to custom events fired when groups are added or removed.
Event Logging
Add or remove an ingredient to see its event name and current group count. Your application can listen for these events without inspecting the generated buttons. The event reference describes the full detail object.
Code
<form-repeatable id="event-repeatable">
<div>
<label for="ingredient-1">
Ingredient 1
</label>
<input id="ingredient-1"
type="text"
name="ingredients[]" placeholder="Add ingredient">
</div>
</form-repeatable>
<script>
const repeatable =
document.querySelector('#event-repeatable');
repeatable.addEventListener(
'form-repeatable:added', (e) => {
console.log('Added:', e.detail.groupCount);
});
repeatable.addEventListener(
'form-repeatable:removed', (e) => {
console.log('Removed:', e.detail.groupCount);
});
</script>
Live example
Event Log:
Custom Styling with ::part()
Use CSS ::part() to style the existing controls
and group wrappers. See the
styling reference for all parts
and the page stylesheet snapshot’s limits.
Styled Buttons
This example uses ::part() selectors to
customize button appearance. Both actions keep text
labels and native focus; color is supplementary. Resize
the window below 600px to see Remove stack below the
fields; this is demo CSS, not a runtime requirement.
Code
<style>
form-repeatable.custom::part(button) {
border: 1px solid currentColor;
padding: 0.5rem 1rem;
border-radius: 4px;
}
form-repeatable.custom::part(add-button) {
background: #17652e;
color: #fff;
}
form-repeatable.custom::part(add-button):hover {
background: #114b22;
}
form-repeatable.custom::part(remove-button) {
background: #982b39;
color: #fff;
}
form-repeatable.custom::part(remove-button):hover {
background: #761f2c;
}
form-repeatable.custom::part(group) {
padding: 1rem;
background: Canvas;
color: CanvasText;
border-radius: 4px;
margin-bottom: 0.5rem;
}
@media (max-width: 600px) {
form-repeatable::part(groups) {
grid-template-columns: minmax(0, 1fr);
}
form-repeatable::part(group) {
grid-column: 1 / -1;
grid-template-columns: minmax(0, 1fr);
row-gap: 0.5rem;
}
form-repeatable::part(button) {
max-inline-size: 100%;
white-space: normal;
overflow-wrap: anywhere;
}
}
</style>
<form-repeatable class="custom">
<div>
<label for="task-1">Task 1</label>
<input id="task-1" type="text"
name="tasks[]" placeholder="Enter task">
</div>
</form-repeatable>