Sub topic 2
Text inputs and textarea
This chapter assumes you know basic HTML structure (elements, attributes, nesting) and can open a page in a browser with developer tools. Labels, buttons, and submission are covered in later chapters, so the examples here keep them minimal.
The single-line text input
The input element with type="text" is the default control for short free text. It is a void element, so it has no closing tag. Its current text is stored in the value property.
<form action="/profile" method="post">
<label for="username">Username</label>
<input id="username" name="username" type="text"
minlength="3" maxlength="20"
placeholder="e.g. ada_l" autocomplete="username">
<button type="submit">Save</button>
</form>Attributes you will use most often:
| Attribute | Effect |
|---|---|
| `name` | Key under which the value is submitted. Without it, the control sends nothing. |
| `value` | Initial text shown in the field. |
| `placeholder` | Hint shown while the field is empty. It disappears on typing. |
| `maxlength` | Stops the user from typing more than N characters. |
| `minlength` | Marks the value invalid if it is shorter than N (but not empty). |
| `readonly` | Value cannot be edited, but is still submitted. |
| `disabled` | Value cannot be edited and is not submitted. |
The textarea element
For multi-line text, use textarea. Unlike input, it is not a void element. Its initial value is the text between the tags, not a value attribute.
<label for="bio">About you</label>
<textarea id="bio" name="bio" rows="5" cols="40"
maxlength="500">Write a few sentences here.</textarea>Points that commonly cause errors:
- Do not add a space or newline between the tags unless you want it in the value. A leading newline directly after the opening tag is ignored, but other whitespace is kept.
rowsandcolsset the initial size. Most pages override this with CSS (width,min-height) and may allowresize: vertical.- Line breaks are submitted as
CRLF(\r\n). If you compare lengths on the server, remember that one line break counts as two characters there. wrap="hard"makes the browser insert line breaks at the wrap points on submission. It requirescols. The default,soft, does not.
input versus textarea at a glance
Reading and setting values in JavaScript
Both controls expose a value property. For a textarea, value is the current text. Setting it replaces the content, and the original markup text is available through defaultValue.
const form = document.querySelector('form');
const bio = form.elements.bio;
bio.addEventListener('input', () => {
console.log(`${bio.value.length} characters`);
});
console.log(bio.defaultValue);The input event fires on every change, including paste and drag-and-drop. The change event fires only when the control loses focus after its value changed.
Common mistakes
- Omitting
name, so the field is missing from the submitted data. - Using
value="..."on atextarea. It is ignored. Put the text between the tags. - Relying on
maxlengthas a security measure. Users can bypass it, so the server must validate length too. - Using
type="text"for emails, numbers, or phone numbers. A specialized type gives better keyboards and validation, as the later chapter on input types shows.
Exercise
A form has <textarea name="note" value="Hello"></textarea>. The page shows an empty box. Why, and how do you fix it?
Show answer
A textarea ignores the value attribute. Its initial content is the text between the tags, so write <textarea name="note">Hello</textarea>.
Exercise
Which attribute stops a field from being edited but still includes it in the submitted data: readonly or disabled?
Show answer
readonly. A disabled control is not submitted.