Phenomenon
Standard HTML forms do not recognize contenteditable regions as valid input fields. When the form is submitted via a standard submit button, the value within the editor is ignored, leading to data loss unless handled programmatically.
Reproduction Steps
- Place a
<div contenteditable="true">inside a<form>. - Add a
<button type="submit">. - Type text into the div and click submit.
- Observe that the submitted request payload contains no key/value for the editor content.
Observed Behavior
- Browsers only include elements with a
nameattribute that are part of the “Form-associated elements” category. contenteditableis a global attribute and does not turn an element into a form-associated one.
Expected Behavior
The editor content should be treatable as a form value, ideally via an internal hidden field or the ElementInternals API.
Solution
Use a hidden input field that syncs on every input event:
const editor = document.querySelector('[contenteditable]');
const hiddenInput = document.querySelector('input[name="editor-content"]');
editor.addEventListener('input', () => {
hiddenInput.value = editor.innerHTML;
});