summaryrefslogtreecommitdiff
path: root/devdocs/html/element%2Finput%2Fsearch.html
blob: e72af5b561bab9187d362c5cd7c7701553bfb1b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<header><h1>&lt;input type="search"&gt;</h1></header><div class="section-content"><p><a href="../input"><code>&lt;input&gt;</code></a> elements of type <code>search</code> are text fields designed for the user to enter search queries into. These are functionally identical to <a href="text"><code>text</code></a> inputs, but may be styled differently by the <a href="https://developer.mozilla.org/en-US/docs/Glossary/User_agent">user agent</a>.</p></div>
<h2 id="try_it">Try it</h2>
<div class="section-content"><iframe class="interactive is-tabbed-standard-height" height="200" src="https://interactive-examples.mdn.mozilla.net/pages/tabbed/input-search.html" title="MDN Web Docs Interactive Example" loading="lazy"></iframe></div>
<h2 id="value">Value</h2>
<div class="section-content">
<p>The <a href="../input#value"><code>value</code></a> attribute contains a string representing the value contained in the search field. You can retrieve this using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement#value"><code>HTMLInputElement.value</code></a> property in JavaScript.</p> <div class="code-example">
<p class="example-header"><span class="language-name">js</span></p>
<pre data-signature="qxctvJ13g/WMY4Gp2AY41+L06WjBxLoJsh2Kc+rwbuM=" data-language="js">searchTerms <span class="token operator">=</span> mySearch<span class="token punctuation">.</span>value<span class="token punctuation">;</span>
</pre>
</div> <p>If no validation constraints are in place for the input (see <a href="#validation">Validation</a> for more details), the value can be any text string or an empty string (<code>""</code>).</p>
</div>
<h2 id="additional_attributes">Additional attributes</h2>
<div class="section-content"><p>In addition to the attributes that operate on all <a href="../input"><code>&lt;input&gt;</code></a> elements regardless of their type, search field inputs support the following attributes.</p></div>
<h3 id="list">list</h3>
<div class="section-content"><p>The values of the list attribute is the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/id"><code>id</code></a> of a <a href="../datalist"><code>&lt;datalist&gt;</code></a> element located in the same document. The <a href="../datalist"><code>&lt;datalist&gt;</code></a> provides a list of predefined values to suggest to the user for this input. Any values in the list that are not compatible with the <a href="../input#type"><code>type</code></a> are not included in the suggested options. The values provided are suggestions, not requirements: users can select from this predefined list or provide a different value.</p></div>
<h3 id="maxlength">maxlength</h3>
<div class="section-content">
<p>The maximum string length (measured in UTF-16 code units) that the user can enter into the search field. This must be an integer value of 0 or higher. If no <code>maxlength</code> is specified, or an invalid value is specified, the search field has no maximum length. This value must also be greater than or equal to the value of <code>minlength</code>.</p> <p>The input will fail <a href="../../constraint_validation">constraint validation</a> if the length of the text entered into the field is greater than <code>maxlength</code> UTF-16 code units long.</p>
</div>
<h3 id="minlength">minlength</h3>
<div class="section-content">
<p>The minimum string length (measured in UTF-16 code units) that the user can enter into the search field. This must be a non-negative integer value smaller than or equal to the value specified by <code>maxlength</code>. If no <code>minlength</code> is specified, or an invalid value is specified, the search input has no minimum length.</p> <p>The search field will fail <a href="../../constraint_validation">constraint validation</a> if the length of the text entered into the field is fewer than <code>minlength</code> UTF-16 code units long.</p>
</div>
<h3 id="pattern">pattern</h3>
<div class="section-content">
<p>The <code>pattern</code> attribute, when specified, is a regular expression that the input's <a href="../input#value"><code>value</code></a> must match for the value to pass <a href="../../constraint_validation">constraint validation</a>. It must be a valid JavaScript regular expression, as used by the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp"><code>RegExp</code></a> type, and as documented in our <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions">guide on regular expressions</a>; the <code>'u'</code> flag is specified when compiling the regular expression so that the pattern is treated as a sequence of Unicode code points, instead of as <a href="https://developer.mozilla.org/en-US/docs/Glossary/ASCII">ASCII</a>. No forward slashes should be specified around the pattern text.</p> <p>If the specified pattern is not specified or is invalid, no regular expression is applied and this attribute is ignored completely.</p> <div class="notecard note" id="sect1"> <p><strong>Note:</strong> Use the <a href="../input#title"><code>title</code></a> attribute to specify text that most browsers will display as a tooltip to explain what the requirements are to match the pattern. You should also include other explanatory text nearby.</p> </div> <p>See the section <a href="#specifying_a_pattern">Specifying a pattern</a> for details and an example.</p>
</div>
<h3 id="placeholder">placeholder</h3>
<div class="section-content">
<p>The <code>placeholder</code> attribute is a string that provides a brief hint to the user as to what kind of information is expected in the field. It should be a word or short phrase that demonstrates the expected type of data, rather than an explanatory message. The text <em>must not</em> include carriage returns or line feeds.</p> <p>If the control's content has one directionality (<a href="https://developer.mozilla.org/en-US/docs/Glossary/LTR">LTR</a> or <a href="https://developer.mozilla.org/en-US/docs/Glossary/RTL">RTL</a>) but needs to present the placeholder in the opposite directionality, you can use Unicode bidirectional algorithm formatting characters to override directionality within the placeholder; see <a href="https://www.w3.org/International/questions/qa-bidi-unicode-controls" target="_blank">How to use Unicode controls for bidi text</a> for more information.</p> <div class="notecard note" id="sect2"> <p><strong>Note:</strong> Avoid using the <code>placeholder</code> attribute if you can. It is not as semantically useful as other ways to explain your form, and can cause unexpected technical issues with your content. See <a href="../input#labels"><code>&lt;input&gt;</code> labels</a> for more information.</p> </div>
</div>
<h3 id="readonly">readonly</h3>
<div class="section-content">
<p>A Boolean attribute which, if present, means this field cannot be edited by the user. Its <code>value</code> can, however, still be changed by JavaScript code directly setting the <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement"><code>HTMLInputElement</code></a> <code>value</code> property.</p> <div class="notecard note" id="sect3"> <p><strong>Note:</strong> Because a read-only field cannot have a value, <code>required</code> does not have any effect on inputs with the <code>readonly</code> attribute also specified.</p> </div>
</div>
<h3 id="size">size</h3>
<div class="section-content">
<p>The <code>size</code> attribute is a numeric value indicating how many characters wide the input field should be. The value must be a number greater than zero, and the default value is 20. Since character widths vary, this may or may not be exact and should not be relied upon to be so; the resulting input may be narrower or wider than the specified number of characters, depending on the characters and the font (<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/font"><code>font</code></a> settings in use).</p> <p>This does <em>not</em> set a limit on how many characters the user can enter into the field. It only specifies approximately how many can be seen at a time. To set an upper limit on the length of the input data, use the <a href="#maxlength"><code>maxlength</code></a> attribute.</p>
</div>
<h3 id="spellcheck">spellcheck</h3>
<div class="section-content">
<p><code>spellcheck</code> is a global attribute which is used to indicate whether to enable spell checking for an element. It can be used on any editable content, but here we consider specifics related to the use of <code>spellcheck</code> on <a href="../input"><code>&lt;input&gt;</code></a> elements. The permitted values for <code>spellcheck</code> are:</p> <dl> <dt id="false"><a href="#false"><code>false</code></a></dt> <dd> <p>Disable spell checking for this element.</p> </dd> <dt id="true"><a href="#true"><code>true</code></a></dt> <dd> <p>Enable spell checking for this element.</p> </dd> <dt id="_empty_string_or_no_value"><a href="#_empty_string_or_no_value">"" (empty string) or no value</a></dt> <dd> <p>Follow the element's default behavior for spell checking. This may be based upon a parent's <code>spellcheck</code> setting or other factors.</p> </dd> </dl> <p>An input field can have spell checking enabled if it doesn't have the <a href="#readonly">readonly</a> attribute set and is not disabled.</p> <p>The value returned by reading <code>spellcheck</code> may not reflect the actual state of spell checking within a control, if the <a href="https://developer.mozilla.org/en-US/docs/Glossary/User_agent">user agent's</a> preferences override the setting.</p>
</div>
<h2 id="non-standard_attributes">Non-standard attributes</h2>
<div class="section-content"><p>The following non-standard attributes are available to search input fields. As a general rule, you should avoid using them unless it can't be helped.</p></div>
<h3 id="autocorrect">autocorrect</h3>
<div class="section-content">
<p>A Safari extension, the <code>autocorrect</code> attribute is a string which indicates whether to activate automatic correction while the user is editing this field. Permitted values are:</p> <dl> <dt id="on"><a href="#on"><code>on</code></a></dt> <dd> <p>Enable automatic correction of typos, as well as processing of text substitutions if any are configured.</p> </dd> <dt id="off"><a href="#off"><code>off</code></a></dt> <dd> <p>Disable automatic correction and text substitutions.</p> </dd> </dl>
</div>
<h3 id="incremental">incremental</h3>
<div class="section-content">
<p>The Boolean attribute <code>incremental</code> is a WebKit and Blink extension (so supported by Safari, Opera, Chrome, etc.) which, if present, tells the <a href="https://developer.mozilla.org/en-US/docs/Glossary/User_agent">user agent</a> to process the input as a live search. As the user edits the value of the field, the user agent sends <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/search_event"><code>search</code></a> events to the <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement"><code>HTMLInputElement</code></a> object representing the search box. This allows your code to update the search results in real time as the user edits the search.</p> <p>If <code>incremental</code> is not specified, the <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/search_event"><code>search</code></a> event is only sent when the user explicitly initiates a search (such as by pressing the <kbd>Enter</kbd> or <kbd>Return</kbd> key while editing the field).</p> <p>The <code>search</code> event is rate-limited so that it is not sent more frequently than an implementation-defined interval.</p>
</div>
<h3 id="mozactionhint_deprecated">mozactionhint <abbr class="icon icon-deprecated" title="Deprecated. Not for use in new websites."> <span class="visually-hidden">Deprecated</span> </abbr>
</h3>
<div class="section-content">
<p>A Mozilla extension, which provides a hint as to what sort of action will be taken if the user presses the <kbd>Enter</kbd> or <kbd>Return</kbd> key while editing the field.</p> <p><strong>Deprecated: Use <a href="../../global_attributes#enterkeyhint"><code>enterkeyhint</code></a> instead.</strong></p>
</div>
<h3 id="results">results</h3>
<div class="section-content">
<p>The <code>results</code> attribute—supported only by Safari—is a numeric value that lets you override the maximum number of entries to be displayed in the <a href="../input"><code>&lt;input&gt;</code></a> element's natively-provided drop-down menu of previous search queries.</p> <p>The value must be a non-negative decimal number. If not provided, or an invalid value is given, the browser's default maximum number of entries is used.</p>
</div>
<h2 id="using_search_inputs">Using search inputs</h2>
<div class="section-content"><p><code>&lt;input&gt;</code> elements of type <code>search</code> are very similar to those of type <code>text</code>, except that they are specifically intended for handling search terms. They are basically equivalent in behavior, but user agents may choose to style them differently by default (and, of course, sites may use stylesheets to apply custom styles to them).</p></div>
<h3 id="basic_example">Basic example</h3>
<div class="section-content">
<div class="code-example">
<p class="example-header"><span class="language-name">html</span></p>
<pre data-signature="NAQqmdLIV0CJzTz0A4Ir4GCprLf2Ef2o6X11v7wDVjo=" data-language="html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>search<span class="token punctuation">"</span></span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mySearch<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>q<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span><span class="token punctuation">&gt;</span></span>Search<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">&gt;</span></span>
</pre>
</div> <p>This renders like so:</p>
<div class="code-example" id="sect4">

<iframe class="sample-code-frame" title="Basic example sample" id="frame_basic_example" width="600" height="60" src="https://live.mdnplay.dev/en-US/docs/Web/HTML/Element/input/search/runner.html?id=basic_example" loading="lazy"></iframe>
</div> <p><code>q</code> is the most common <code>name</code> given to search inputs, although it's not mandatory. When submitted, the data name/value pair sent to the server will be <code>q=searchterm</code>.</p> <div class="notecard note" id="sect5"> <p><strong>Note:</strong> You must remember to set a <a href="../input#name"><code>name</code></a> for your input, otherwise nothing will be submitted.</p> </div>
</div>
<h3 id="differences_between_search_and_text_types">Differences between search and text types</h3>
<div class="section-content">
<p>The main basic differences come in the way browsers handle them. The first thing to note is that some browsers show a cross icon that can be clicked on to remove the search term instantly if desired, in Chrome this action is also triggered when pressing escape. The following screenshot comes from Chrome:</p> <p> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJAAAAAfCAMAAAA/frSHAAAA1VBMVEX///8AAAC93v7N5f5wrurIyMjd3d273f7n8v+Dwf2u0PLd7f7y+P/29vbNzc3o8/+Pxv1xr+t/vfl1s+/7/v+53P3t6+39//Px8fHs7OyPuOHWmUb416n/+/7J7v/958C/iEXao1T/9e8ACVIAUaAAAzHV9v8rm9YslNRJnta42vsAe72mXQDm+/8BEncyAgAAhMa2dQDf1dKRMwDPhgD/+OOmj5RPBQDXsoQAab201+8ANHu84/fv7/eKy+/HopSlknsAhM7/9s57i6VkTYR7eXODpr21XoQkAAABWUlEQVRYw+2XR3ODMBCFV2ABAtOxcaHYMdhJ3Gt6nP7/f1IowcmBTE5E8ozfQbO6fbP7JD0BnHSUqusSben1bxxH4TmRtjhecQoghTMIfRmcUsyLN4hlmzWaMm2LGPzX1HSOWB25QXVgDbljEU7PgSSR2DJ1U8s2EaUDkNmQKJ9z6dL8CVQT6QCpgoY1Qc0gagwAtXGu9t9Anvwf/cGF1HKgsyYU62aRVg/VAgkYtwQstDAWSoG6CA146CVr0EcocpJqNqwSSEtJmimXVgbkDQLoPW93Q9iMoHsOL/MA9vdVAuX2yYxUBtQfZ7tw9351kQK56/iVh8o71P61Q/2bpPZD9LFaZh0CWD2hR4oeCuNbf7nwZtPtOunQGMK3oTsZ0Txl1wjdBe4ExcnIvHnkJqaOplTvIT8LJP4hJvkOtZuaubeMrdeeuTzEXGJkLlOz9+tg7l920lHpE8PzSXV8Ppl+AAAAAElFTkSuQmCC" alt="Focused search input, with focus ring, with the text 'cats'. There is an x icon in the input abutting the right side." width="144" height="31" loading="lazy"> </p> <p>In addition, modern browsers also tend to automatically store search terms previously entered across domains, which then come up as autocomplete options when subsequent searches are performed in search inputs on that domain. This helps users who tend to do searches on the same or similar search queries over time. This screenshot is from Firefox:</p> <p> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKsAAABTCAMAAADX9wdfAAACIlBMVEX/////4+Lvsrnz8vL8/Pz7+vr//f3+/v79/f3o6Oj39/f/9PT/zMvu7u6Cs+Ogoc+w1/35+fmmyu7Ozs7Y2NjExMSwsLC6urqenp5tl7+np6eQkJB1dXXg4ODi4eGpqanIyMjX19f//tcAAADO9//7+/v29vbt7e1ICkf3///b29v4+Pnv7+/09PTf39/q6urw8PCurq7Ly8v///f99PTU1NTj4+Pl5eXx8fLBwcH++/vR0dG0tLTi4uL+y8r/9/eV1/fIx8ejoqT//uuVlZW4uLj/9s/XnkWfoM6u1vva2tpumMG8vb3//v6BsuLY/v+hMwCEhIQACE96enr71Z3/5+dwm8T909L94+L7vrzrrrYomddIBAAwAgCmur4AUKnYpkeRDwAAeLqpNAD15+Wpz/AAN59Dn9YCGpzW9/+12PH/7Oz/29qwkmzYnqHtzKe3osSn3/5gBwBOqtsAAy9zpM7937EABkfGqpTO6fv45cmlcCZQDEdPXHlXTU/v//+45/tQlbf97s338PB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dbvdAMRHxeKBG63JJ1IK1DGJLbJALvS4tKYEWHYTaSuRU9HNHTZ6Lfk1GTmx5qKZsgfo3Tf13QmSPYx4AAAAASUVORK5CYII=" alt="An input in error state with a red focus ring. The user has entered the letter 'h'. A pop-up selection list is open directly under the input box with two options: hello and hermansje." width="171" height="83" loading="lazy">At this point, let's look at some useful techniques you can apply to your search forms. </p>
</div>
<h3 id="setting_placeholders">Setting placeholders</h3>
<div class="section-content">
<p>You can provide a useful placeholder inside your search input that could give a hint on what to do using the <a href="../input#placeholder"><code>placeholder</code></a> attribute. Look at the following example:</p> <div class="code-example">
<p class="example-header"><span class="language-name">html</span></p>
<pre data-signature="E4kIkFAXgyJyTEg2+c5DZGZhsOYLadNJbePvlgYbwYs=" data-language="html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span>
      <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>search<span class="token punctuation">"</span></span>
      <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mySearch<span class="token punctuation">"</span></span>
      <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>q<span class="token punctuation">"</span></span>
      <span class="token attr-name">placeholder</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>Search the site…<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span><span class="token punctuation">&gt;</span></span>Search<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">&gt;</span></span>
</pre>
</div> <p>You can see how the placeholder is rendered below:</p>
<div class="code-example" id="sect6">

<iframe class="sample-code-frame" title="Setting placeholders sample" id="frame_setting_placeholders" width="600" height="60" src="https://live.mdnplay.dev/en-US/docs/Web/HTML/Element/input/search/runner.html?id=setting_placeholders" loading="lazy"></iframe>
</div>
</div>
<h3 id="search_form_labels_and_accessibility">Search form labels and accessibility</h3>
<div class="section-content">
<p>One problem with search forms is their accessibility; a common design practice is not to provide a label for the search field (although there might be a magnifying glass icon or similar), as the purpose of a search form is normally fairly obvious for sighted users due to placement (<a href="https://mdn.github.io/learning-area/accessibility/aria/website-aria-roles/" target="_blank">this example shows a typical pattern</a>).</p> <p>This could, however, cause confusion for screen reader users, since they will not have any verbal indication of what the search input is. One way around this that won't impact on your visual design is to use <a href="https://developer.mozilla.org/en-US/docs/Learn/Accessibility/WAI-ARIA_basics">WAI-ARIA</a> features:</p> <ul> <li>A <code>role</code> attribute of value <code>search</code> on the <code>&lt;form&gt;</code> element will cause screen readers to announce that the form is a search form.</li> <li>If that isn't enough, you can use an <a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-label"><code>aria-label</code></a> attribute on the <a href="../input"><code>&lt;input&gt;</code></a> itself. This should be a descriptive text label that will be read out by the screen reader; it's used as a non-visual equivalent to <code>&lt;label&gt;</code>.</li> </ul> <p>Let's have a look at an example:</p> <div class="code-example">
<p class="example-header"><span class="language-name">html</span></p>
<pre data-signature="z+WPM2cqyaueYg00nAB0Wo4IwM6GjIQ2kRoP0WnJyRw=" data-language="html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span> <span class="token attr-name">role</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>search<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span>
      <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>search<span class="token punctuation">"</span></span>
      <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mySearch<span class="token punctuation">"</span></span>
      <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>q<span class="token punctuation">"</span></span>
      <span class="token attr-name">placeholder</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>Search the site…<span class="token punctuation">"</span></span>
      <span class="token attr-name">aria-label</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>Search through site content<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span><span class="token punctuation">&gt;</span></span>Search<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">&gt;</span></span>
</pre>
</div> <p>You can see how this is rendered below:</p>
<div class="code-example" id="sect7">

<iframe class="sample-code-frame" title="Search form labels and accessibility sample" id="frame_search_form_labels_and_accessibility" width="600" height="60" src="https://live.mdnplay.dev/en-US/docs/Web/HTML/Element/input/search/runner.html?id=search_form_labels_and_accessibility" loading="lazy"></iframe>
</div> <p>There is no visual difference from the previous example, but screen reader users have way more information available to them.</p> <div class="notecard note" id="sect8"> <p><strong>Note:</strong> See <a href="https://developer.mozilla.org/en-US/docs/Learn/Accessibility/WAI-ARIA_basics#signpostslandmarks">Signposts/Landmarks</a> for more information about such accessibility features.</p> </div>
</div>
<h3 id="physical_input_element_size">Physical input element size</h3>
<div class="section-content">
<p>The physical size of the input box can be controlled using the <a href="../input#size"><code>size</code></a> attribute. With it, you can specify the number of characters the input box can display at a time. In this example, for instance, the search box is 30 characters wide:</p> <div class="code-example">
<p class="example-header"><span class="language-name">html</span></p>
<pre data-signature="iw6lObmkzq4stssnZKn6gv3ei3pvRs3pw6D0FUSXFHE=" data-language="html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span>
      <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>search<span class="token punctuation">"</span></span>
      <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mySearch<span class="token punctuation">"</span></span>
      <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>q<span class="token punctuation">"</span></span>
      <span class="token attr-name">placeholder</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>Search the site…<span class="token punctuation">"</span></span>
      <span class="token attr-name">size</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>30<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span><span class="token punctuation">&gt;</span></span>Search<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">&gt;</span></span>
</pre>
</div> <p>The result is this wider input box:</p>
<div class="code-example" id="sect9">

<iframe class="sample-code-frame" title="Physical input element size sample" id="frame_physical_input_element_size" width="600" height="60" src="https://live.mdnplay.dev/en-US/docs/Web/HTML/Element/input/search/runner.html?id=physical_input_element_size" loading="lazy"></iframe>
</div>
</div>
<h2 id="validation">Validation</h2>
<div class="section-content">
<p><code>&lt;input&gt;</code> elements of type <code>search</code> have the same validation features available to them as regular <code>text</code> inputs. It is less likely that you'd want to use validation features in general for search boxes. In many cases, users should just be allowed to search for anything, but there are a few cases to consider, such as searches against data of a known format.</p> <div class="notecard note" id="sect10"> <p><strong>Note:</strong> HTML form validation is <em>not</em> a substitute for scripts that ensure that the entered data is in the proper format. It's far too easy for someone to make adjustments to the HTML that allow them to bypass the validation, or to remove it entirely. It's also possible for someone to bypass your HTML entirely and submit the data directly to your server. If your server-side code fails to validate the data it receives, disaster could strike when improperly-formatted data (or data which is too large, is of the wrong type, and so forth) is entered into your database.</p> </div>
</div>
<h3 id="a_note_on_styling">A note on styling</h3>
<div class="section-content">
<p>There are useful pseudo-classes available for styling valid/invalid form elements: <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:valid"><code>:valid</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/:invalid"><code>:invalid</code></a>. In this section, we'll use the following CSS, which will place a check (tick) next to inputs containing valid values, and a cross next to inputs containing invalid values.</p> <div class="code-example">
<p class="example-header"><span class="language-name">css</span></p>
<pre data-signature="CKRTwu8HzX0yTXvAMib+eMKbf76PT5ZKisJwHlwijwc=" data-language="css"><span class="token selector">input:invalid ~ span::after</span> <span class="token punctuation">{</span>
  <span class="token property">content</span><span class="token punctuation">:</span> <span class="token string">"✖"</span><span class="token punctuation">;</span>
  <span class="token property">padding-left</span><span class="token punctuation">:</span> 5px<span class="token punctuation">;</span>
  <span class="token property">position</span><span class="token punctuation">:</span> absolute<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token selector">input:valid ~ span::after</span> <span class="token punctuation">{</span>
  <span class="token property">content</span><span class="token punctuation">:</span> <span class="token string">"✓"</span><span class="token punctuation">;</span>
  <span class="token property">padding-left</span><span class="token punctuation">:</span> 5px<span class="token punctuation">;</span>
  <span class="token property">position</span><span class="token punctuation">:</span> absolute<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</pre>
</div> <p>The technique also requires a <a href="../span"><code>&lt;span&gt;</code></a> element to be placed after the form element, which acts as a holder for the icons. This was necessary because some input types on some browsers don't display icons placed directly after them very well.</p>
</div>
<h3 id="making_input_required">Making input required</h3>
<div class="section-content">
<p>You can use the <a href="../input#required"><code>required</code></a> attribute as an easy way of making entering a value required before form submission is allowed:</p> <div class="code-example">
<p class="example-header"><span class="language-name">html</span></p>
<pre data-signature="iNjkpirg8ZCYqbUHWf7YcqdVMUJWOAZ9fb0pTa4CMqw=" data-language="html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span>
      <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>search<span class="token punctuation">"</span></span>
      <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mySearch<span class="token punctuation">"</span></span>
      <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>q<span class="token punctuation">"</span></span>
      <span class="token attr-name">placeholder</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>Search the site…<span class="token punctuation">"</span></span>
      <span class="token attr-name">required</span> <span class="token punctuation">/&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span><span class="token punctuation">&gt;</span></span>Search<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>validity<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">&gt;</span></span>
</pre>
</div>  <p>This renders like so:</p>
<div class="code-example" id="sect11">

<iframe class="sample-code-frame" title="Making input required sample" id="frame_making_input_required" width="600" height="60" src="https://live.mdnplay.dev/en-US/docs/Web/HTML/Element/input/search/runner.html?id=making_input_required" loading="lazy"></iframe>
</div> <p>In addition, if you try to submit the form with no search term entered into it, the browser will show a message. The following example is from Firefox:</p> <p> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANEAAAByCAMAAAD6bMSdAAAB11BMVEX////q6uqw1/35+fn//v79/f339/f7+/v8/Pz6+vrGxsb09PXx8fHu7u6Cs+P/4+L/zMugoc///Pz/9PTvsrnl5eX08/Ph4eHc3NzR0dHX19ekpKT/ysmfoM7+4N/usbj78PCBsuL49fX4+Pi2trb19fXy8vLZ2dn29vbr6+vm5ubk5OTv7+8aGhp3d3enpqbx8PHW1tbo6Ona2trs7Ozu7e3Nzc3q//9bW1v///fR1NbExMT//f3//++/wL4rKyu7u7vb29uw1fj/5+Xk4+OayeiFhYX/9/fKysrJycmqqqineXjj9/+4uLjp6en/8/Pf3988PDyNjY359/o/Pz+SkpLQ0ND++/v77ez3//+tra3e3t7729r4xsX/9+RycnJCQkI2NjZ6enp/sN/g4OGY0u/w16/XpndpaWnv0KCbnJ1MTEyhep95eZvrrLPor7W1ocNmZmbK6/ykoKB3ptT++fn+79b//uexsbGUlJTZ7fz/7cd2l8B0wuV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wwJKpA5dZ7Z/IOIblRR4vIhIJBnud12hHqmgiGGOBhKbcRtxmU6zLAjSAc7muhA6IoMloS+IM6aljK4a6gFQTNJQgTQFEs60SitSHVFxa3PRm5fLsOQewBSe12Q1SqKG0IO4NauDgu21NVrnM63zGKesSAijNqTZX1iO3s4yPRR0K91oScAt9M2Qaxss3thb2WJo1e1zJV2K2/o79XkzpS8V9UKGxa1qYq/6sXhmpQpX/G4RPD0R4hUVNNNdX0wfo/kd+DPVg5yWUAAAAASUVORK5CYII=" alt="form field with attached message that says Please fill out this field" width="209" height="114" loading="lazy"> </p> <p>Different messages will be shown when you try to submit the form with different types of invalid data contained inside the inputs; see the below examples.</p>
</div>
<h3 id="input_value_length">Input value length</h3>
<div class="section-content">
<p>You can specify a minimum length, in characters, for the entered value using the <a href="../input#minlength"><code>minlength</code></a> attribute; similarly, use <a href="../input#maxlength"><code>maxlength</code></a> to set the maximum length of the entered value.</p> <p>The example below requires that the entered value be 4–8 characters in length.</p> <div class="code-example">
<p class="example-header"><span class="language-name">html</span></p>
<pre data-signature="ZyMITUb28g7lxUjiFVL9MvpP+txnUKKeGuIwpaozwfY=" data-language="html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>label</span> <span class="token attr-name">for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mySearch<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>Search for user<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>label</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span>
      <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>search<span class="token punctuation">"</span></span>
      <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mySearch<span class="token punctuation">"</span></span>
      <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>q<span class="token punctuation">"</span></span>
      <span class="token attr-name">placeholder</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>User IDs are 4–8 characters in length<span class="token punctuation">"</span></span>
      <span class="token attr-name">required</span>
      <span class="token attr-name">size</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>30<span class="token punctuation">"</span></span>
      <span class="token attr-name">minlength</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>4<span class="token punctuation">"</span></span>
      <span class="token attr-name">maxlength</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>8<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span><span class="token punctuation">&gt;</span></span>Search<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>validity<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">&gt;</span></span>
</pre>
</div>  <p>This renders like so:</p>
<div class="code-example" id="sect12">

<iframe class="sample-code-frame" title="Input value length sample" id="frame_input_value_length" width="600" height="60" src="https://live.mdnplay.dev/en-US/docs/Web/HTML/Element/input/search/runner.html?id=input_value_length" loading="lazy"></iframe>
</div> <p>If you try to submit the form with less than 4 characters, you'll be given an appropriate error message (which differs between browsers). If you try to go beyond 8 characters in length, the browser won't let you.</p>
</div>
<h3 id="specifying_a_pattern">Specifying a pattern</h3>
<div class="section-content">
<p>You can use the <a href="../input#pattern"><code>pattern</code></a> attribute to specify a regular expression that the inputted value must follow to be considered valid (see <a href="https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation#validating_against_a_regular_expression">Validating against a regular expression</a> for a simple crash course).</p> <p>Let's look at an example. Say we wanted to provide a product ID search form, and the IDs were all codes of two letters followed by four numbers. The following example covers it:</p> <div class="code-example">
<p class="example-header"><span class="language-name">html</span></p>
<pre data-signature="RV6Kk6CwFhuerNj8b2cbJRsYyK6EpKsqPLPz+O9TvCU=" data-language="html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>label</span> <span class="token attr-name">for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mySearch<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>Search for product by ID:<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>label</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span>
      <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>search<span class="token punctuation">"</span></span>
      <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mySearch<span class="token punctuation">"</span></span>
      <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>q<span class="token punctuation">"</span></span>
      <span class="token attr-name">placeholder</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>two letters followed by four numbers<span class="token punctuation">"</span></span>
      <span class="token attr-name">required</span>
      <span class="token attr-name">size</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>30<span class="token punctuation">"</span></span>
      <span class="token attr-name">pattern</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>[A-z]{2}[0-9]{4}<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span><span class="token punctuation">&gt;</span></span>Search<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>validity<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">&gt;</span></span>
</pre>
</div>  <p>This renders like so:</p>
<div class="code-example" id="sect13">

<iframe class="sample-code-frame" title="Specifying a pattern sample" id="frame_specifying_a_pattern" width="600" height="60" src="https://live.mdnplay.dev/en-US/docs/Web/HTML/Element/input/search/runner.html?id=specifying_a_pattern" loading="lazy"></iframe>
</div>
</div>
<h2 id="examples">Examples</h2>
<div class="section-content"><p>You can see a good example of a search form used in context at our <a href="https://github.com/mdn/learning-area/tree/main/accessibility/aria/website-aria-roles" target="_blank">website-aria-roles</a> example (<a href="https://mdn.github.io/learning-area/accessibility/aria/website-aria-roles/" target="_blank">see it live</a>).</p></div>
<h2 id="technical_summary">Technical summary</h2>
<div class="section-content"><figure class="table-container"><div class="_table"><table class="properties"> <tbody> <tr> <td><strong><a href="#value">Value</a></strong></td> <td> A string representing the value contained in the search field. </td> </tr> <tr> <td><strong>Events</strong></td> <td> <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event"><code>change</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event"><code>input</code></a> </td> </tr> <tr> <td><strong>Supported Common Attributes</strong></td> <td> <a href="../input#autocomplete"><code>autocomplete</code></a>, <a href="../input#list"><code>list</code></a>, <a href="../input#maxlength"><code>maxlength</code></a>, <a href="../input#minlength"><code>minlength</code></a>, <a href="../input#pattern"><code>pattern</code></a>, <a href="../input#placeholder"><code>placeholder</code></a>, <a href="../input#required"><code>required</code></a>, <a href="../input#size"><code>size</code></a>. </td> </tr> <tr> <td><strong>IDL attributes</strong></td> <td><code>value</code></td> </tr> <tr> <td><strong>DOM interface</strong></td> <td> <p><a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement"><code>HTMLInputElement</code></a></p> </td> </tr> <tr> <td><strong>Methods</strong></td> <td> <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select"><code>select()</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setRangeText"><code>setRangeText()</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange"><code>setSelectionRange()</code></a>. </td> </tr> <tr> <td><strong>Implicit ARIA Role</strong></td> <td>with no <code>list</code> attribute: <code><a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/searchbox_role">searchbox</a></code>
</td> <td>with <code>list</code> attribute: <code><a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/combobox_role">combobox</a></code>
</td> </tr> </tbody> </table></div></figure></div>
<h2 id="specifications">Specifications</h2>
<div class="_table"><table class="standard-table">
<thead><tr><th scope="col">Specification</th></tr></thead>
<tbody><tr><td><a href="https://html.spec.whatwg.org/multipage/input.html#text-(type=text)-state-and-search-state-(type=search)">HTML Standard <br><small># text-(type=text)-state-and-search-state-(type=search)</small></a></td></tr></tbody>
</table></div>
<h2 id="browser_compatibility">Browser compatibility</h2>
<div class="_table"><table>
<thead>
<tr id="bct-browser-type">
<th></th>
<th colspan="6">Desktop</th>
<th colspan="6">Mobile</th>
</tr>
<tr id="bct-browsers">
<th></th>
<th>Chrome</th>
<th>Edge</th>
<th>Firefox</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
<th>WebView Android</th>
<th>Chrome Android</th>
<th>Firefox for Android</th>
<th>Opera Android</th>
<th>Safari on IOS</th>
<th>Samsung Internet</th>
</tr>
</thead>
<tbody><tr>
<th><code>search</code></th>
<td class="bc-supports-yes">5</td>
<td class="bc-supports-yes">12</td>
<td class="bc-supports-yes">4</td>
<td class="bc-supports-yes">10</td>
<td class="bc-supports-yes">10.6</td>
<td class="bc-supports-yes">5</td>
<td class="bc-supports-yes">4.4</td>
<td class="bc-supports-yes">18</td>
<td class="bc-supports-yes">4</td>
<td class="bc-supports-yes">14</td>
<td class="bc-supports-yes">4.2</td>
<td class="bc-supports-yes">1.0</td>
</tr></tbody>
</table></div>
<h2 id="see_also">See also</h2>
<div class="section-content"><ul> <li><a href="https://developer.mozilla.org/en-US/docs/Learn/Forms">HTML Forms</a></li> <li>
<a href="../input"><code>&lt;input&gt;</code></a> and the <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement"><code>HTMLInputElement</code></a> interface it's based upon</li> <li><a href="text"><code>&lt;input type="text"&gt;</code></a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Learn/Forms/Property_compatibility_table_for_form_controls">Compatibility of CSS properties</a></li> </ul></div><div class="_attribution">
  <p class="_attribution-p">
    &copy; 2005&ndash;2023 MDN contributors.<br>Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.<br>
    <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/search" class="_attribution-link">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/search</a>
  </p>
</div>