Sub topic 2
Text inputs and textarea
This chapter assumes you can write a basic <form> with an action and method, and that you know how HTML attributes and nesting work. It covers the two controls that collect free text: the single-line <input> and the multi-line <textarea>. Labels, specialized input types, and validation get their own chapters. They appear here only where they affect text entry.
The single-line text input
The <input> element is a void element: it has no closing tag and no content. Its type attribute selects the control. When type is omitted, or has a value the browser doesn't recognize, the browser uses type="text".
<form action="/profile" method="post">
<label for="display-name">Display name</label>
<input id="display-name" name="display_name" type="text"
maxlength="40" autocomplete="nickname">
<button type="submit">Save</button>
</form>Three things happen when the user submits this form:
- The browser reads the control's current value.
- It pairs the value with the
nameattribute. - It sends
display_name=<value>in the request.
A control with no name is never submitted, even if the user typed into it. Missing name attributes are a common cause of "my field arrives empty on the server" bugs. A control that has a name but no text still submits an empty pair, such as display_name=.
A single-line input cannot hold a line break. If the user pastes multi-line text into it, the browser strips the newlines from the value.
Attributes that shape a text input
| Attribute | Effect |
|---|---|
| `value` | The initial value shown when the page loads. |
| `placeholder` | Hint text shown while the field is empty. |
| `maxlength` | Maximum length the user can type. Longer pasted text is cut off. |
| `minlength` | Minimum length. It is checked only at validation time, and only after the user has edited the value. |
| `size` | Visible width in characters. CSS `width` is usually a better choice. |
| `readonly` | The user cannot edit the value. It is still submitted. |
| `disabled` | The control is inert, skipped by Tab, and not submitted. |
| `autofocus` | Focuses the control when the page loads. Use it sparingly. |
| `spellcheck` | Turns spell checking on or off, for example off for usernames. |
| `autocapitalize` | Hints to touch keyboards whether to capitalize words or sentences. |
| `inputmode` | Hints which virtual keyboard to show, for example `numeric`. |
maxlength and minlength count UTF-16 code units, not visible characters. An emoji can count as two. If you show a "characters left" number, expect small differences from what users see.
minlength is checked only after the user edits the value. A value set from the value attribute or from a script does not trigger a minlength error until the user changes it.
Placeholder is not a label
A placeholder disappears as soon as the user types. It is usually low-contrast, and screen readers don't treat it as a reliable name for the control. Use it for a short example format, such as placeholder="e.g. Ada Lovelace". Do not use it to say what the field is. The next chapter covers labels.
readonly versus disabled
These two attributes are easy to confuse because both stop editing.
| Behavior | `readonly` | `disabled` |
|---|---|---|
| User can edit | No | No |
| Can receive focus | Yes | No |
| Submitted with the form | Yes | No |
| Checked by validation | No | No |
| Matches `:disabled` in CSS | No | Yes |
Use readonly for a value the user should see, copy, and send, such as a generated reference number. Use disabled for a control that does not apply right now. Don't use either one as a security measure. Users can change both in developer tools, so the server must check every value it receives.
The textarea element
A <textarea> holds multiple lines of text. Unlike <input>, it has a closing tag, and its initial value is its text content, 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>Several details trip people up:
- Always write the closing tag.
<textarea />is not self-closing. The browser treats everything after the opening tag as text until it finds</textarea>. Your whole page can disappear into the field. - A single newline right after the opening tag is dropped. The example above puts the text on its own line for readability, and the browser ignores that first newline. Any other whitespace inside the element is kept. Indentation you add for tidy source code becomes part of the value.
- Content is text, not markup. Write
<for a literal<in initial content. When a server renders user-supplied text back into a textarea, it must escape that text. Otherwise a value containing</textarea>breaks the page and opens the door to script injection. rowsandcolsset the starting size. The defaults are 2 rows and 20 columns. Most projects set the width with CSS and userowsonly for the height.placeholderworks, and can contain line breaks. The same accessibility warning applies.
Line breaks in submitted data
In JavaScript, textarea.value uses \n for line breaks. When the form is submitted, the browser normalizes each line break to \r\n (CRLF). With the default encoding, a two-line value looks like this on the wire:
bio=Line+one%0D%0ALine+twoIf your server compares or counts text, handle \r\n. A common bug is a length check that passes in the browser but fails on the server, because each line break is one character on the client and two on the wire. Normalizing to \n on the server avoids the mismatch.
The wrap attribute
wrap="soft" is the default. Lines wrap on screen, but the submitted value contains only the line breaks the user typed. wrap="hard" makes the browser insert line breaks where text wraps, and it requires cols. Hard wrapping is rarely wanted, because the stored text then depends on the width of the box.
Sizing and resizing
Browsers let users drag the corner of a textarea to resize it. You control this with CSS:
textarea {
width: 100%;
box-sizing: border-box;
resize: vertical; /* allow height changes only, so the layout can't break */
}Newer browsers also support field-sizing: content, which grows the box with its text. Check browser support before relying on it.
A worked example: a feedback form
This page combines both controls with a live character counter. It runs as shown when saved as an .html file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Feedback</title>
<style>
textarea { width: 100%; box-sizing: border-box; resize: vertical; }
</style>
</head>
<body>
<form action="/feedback" method="post">
<label for="subject">Subject</label>
<input id="subject" name="subject" type="text"
maxlength="80" required placeholder="e.g. Search is slow">
<label for="message">Message</label>
<textarea id="message" name="message" rows="6" maxlength="500"></textarea>
<p><output id="count" for="message">500 characters left</output></p>
<button type="submit">Send</button>
</form>
<script>
const message = document.querySelector('#message');
const count = document.querySelector('#count');
message.addEventListener('input', () => {
const left = message.maxLength - message.value.length;
count.textContent = left + ' characters left';
});
</script>
</body>
</html>The input event fires on every change, including typing, pasting, and dragging text in. That makes it a better choice than keyup, which misses mouse and clipboard edits. The counter updates visually only. A screen reader user does not hear each change, which is the right behavior. Announcing every keystroke would be noisy.
The next diagram shows how to pick a control for a piece of text.
Common errors
- Forgetting
name, so the field never reaches the server. - Using
placeholderinstead of a label. - Indenting textarea content in the source, which adds leading spaces to the value.
- Reading a
<textarea>'svalueattribute. Use thevalueproperty in scripts and the text content in markup. - Trusting
maxlength,readonly, ordisabledas server-side rules. - Setting
type="text"for everything. Emails, phone numbers, and similar data benefit from a more specific type, covered in a later chapter.
Exercise
A user types "Hi" on one line and "there" on the next into a textarea named note, then submits a POST form with the default encoding. What does the note pair look like in the request body? What goes wrong if the same control is a readonly versus a disabled field?
Show answer
The body contains note=Hi%0D%0Athere, because the browser normalizes the line break to CRLF and URL-encodes it. A readonly field is still submitted with this pair, while a disabled field is left out of the request entirely.