HTML Text and Search Inputs: Single-Line Fields
Text inputs
Use type="text" for an ordinary single-line string. Omitting type normally selects the text state. The value attribute sets the starting value; users can edit it.
<label> Project <input type="text" name="project" value="Garden"></label>Garden is an actual editable initial value.
Search inputs
type="search" identifies a search query. Some browsers give this control a clear affordance or different keyboard appearance. It does not search by itself; a service or script must perform the search.
<label> Find a topic <input type="search" name="q" placeholder="e.g. tables"></label>The input begins empty even though the hint is visible.
Length and read-only settings
maxlength limits user-entered length in UTF-16 code units, not necessarily the number of visible characters. readonly prevents ordinary user editing but still allows the named control to contribute a value; disabled does not.
<form> <label> Nickname <input name="nick" maxlength="12" size="16"> </label> <label> Member <input name="member" value="A12" readonly> </label></form>Nickname has a length limit; Member cannot be edited normally but remains in form data.
Common mistakes and practice
A placeholder does not supply a submitted value. Text and search are single-line controls. Do not use size as a maximum length: it is a display hint, while maxlength restricts ordinary user entry. Server-side validation is still necessary.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Text and Search Inputs: Single-Line Fields — Knowledge Check
Apply the form-control concepts from this lesson.
Which type identifies a search query?
What does a placeholder contribute when a field is empty?
Which attribute prevents editing but preserves a named field in form data?
Which attribute limits the length of text a user can enter?
A search input needs an initial query visible as its value. Which attribute sets it?