5 <meta name="viewport" content="width=device-width, initial-scale=1">
6 <title>jQuery Mobile Docs - Text inputs</title>
7 <link rel="stylesheet" href="../../../css/themes/default/jquery.mobile.css" />
8 <link rel="stylesheet" href="../../_assets/css/jqm-docs.css"/>
9 <script src="../../../experiments/themeswitcher/jquery.mobile.themeswitcher.js"></script>
10 <script src="../../../js/jquery.js"></script>
11 <script src="../../../docs/_assets/js/jqm-docs.js"></script>
12 <script src="../../../js/"></script>
17 <div data-role="page" class="type-interior">
19 <div data-role="header" data-theme="f">
21 <a href="../../../" data-icon="home" data-iconpos="notext" data-direction="reverse">Home</a>
22 <a href="../../nav.html" data-icon="search" data-iconpos="notext" data-rel="dialog" data-transition="fade">Search</a>
23 </div><!-- /header -->
25 <div data-role="content">
26 <div class="content-primary">
28 <form action="#" method="get">
30 <h2>Text inputs & Textareas</h2>
32 <ul data-role="controlgroup" data-type="horizontal" class="localnav">
33 <li><a href="index.html" data-role="button" data-transition="fade" class="ui-btn-active">Basics</a></li>
34 <li><a href="options.html" data-role="button" data-transition="fade">Options</a></li>
35 <li><a href="methods.html" data-role="button" data-transition="fade">Methods</a></li>
36 <li><a href="events.html" data-role="button" data-transition="fade">Events</a></li>
39 <p>Text inputs and textareas are coded with standard HTML elements, then enhanced by jQuery Mobile to make them more attractive and useable on a mobile device. View the <a href="../../api/data-attributes.html">data- attribute reference</a> to see all the possible attributes you can add to text inputs.</p>
42 <p>To collect standard alphanumeric text, use an <code>input</code> with a <code>type="text"</code> attribute. Set the <code>for</code> attribute of the <code>label</code> to match the ID of the <code>input</code> so they are semantically associated. It's possible to <a href="../docs-forms.html">accessibly hide the label</a> if it's not desired in the page layout, but we require that it is present in the markup for semantic and accessibility reasons.</p>
45 <label for="basic">Text Input:</label>
46 <input type="text" name="name" id="basic" value="" />
49 <p>This will produce a basic text input. The default styles set the width of the input to 100% of the parent container and stack the label on a separate line.</p>
51 <label for="basic">Text Input:</label>
52 <input type="text" name="name" id="basic" value="" />
56 <p>For a more compact version that is useful in toolbars and tight spaces, add the <code>data-mini="true"</code> attribute to the element to create a <a href="../forms-all-mini.html">mini version</a>. </p>
59 <label for="basic">Text Input:</label>
60 <input type="text" name="name" id="basic" value="" <strong>data-mini="true"</strong> />
63 <p>This will produce an input that is not as tall as the standard version and has a smaller text size.</p>
64 <label for="mini">Text Input:</label>
65 <input type="text" name="name" id="mini" value="" data-mini="true" />
67 <h2>Field containers</h2>
69 <p>Optionally wrap the text input in a container with the <code> data-role="fieldcontain"</code> attribute to help visually group it in a longer form.</p>
72 <strong><div data-role="fieldcontain">
73 </strong> <label for="name">Text Input:</label>
74 <input type="text" name="name" id="name" value="" />
76 </strong></code></pre>
78 <p>The text input is now displayed like this:</p>
79 <div data-role="fieldcontain">
80 <label for="name">Text Input:</label>
81 <input type="text" name="name" id="name" value="" />
85 <h2>More text input types</h2>
86 <p>In jQuery Mobile, you can use existing and new HTML5 input types such as <code>password</code>, <code>email</code>, <code>tel</code>, <code>number</code>, and more. Some type values are rendered differently across browsers. For example, Chrome renders the <code>range</code> input as a slider. jQuery Mobile standardizes the appearance of <code>range</code> and <code>search</code> by dynamically changing their type to <code>text</code>. You can configure which input types are degraded to <code>text</code> with the <code>page</code> plugin's options.</p>
88 <p>One major advantage of using these more specific input types if that on mobile devices, specialized keyboards that speed data entry are offered in place of the standard text keyboard. Try the following inputs on a mobile device to see which display custom keyboards on various platforms.</p>
90 <div data-role="fieldcontain">
91 <label for="password">Password:</label>
92 <input type="password" name="password" id="password" value="" />
95 <div data-role="fieldcontain">
96 <label for="number">Number:</label>
97 <input type="number" name="number" id="number" value="" />
100 <div data-role="fieldcontain">
101 <label for="number-pattern">Number + pattern:</label>
102 <input type="number" name="number" pattern="[0-9]*" id="number-pattern" value="" placeholder="Pattern attribute [0-9]* for a numeric keypad" />
105 <div data-role="fieldcontain">
106 <label for="email">Email:</label>
107 <input type="email" name="email" id="email" value="" />
110 <div data-role="fieldcontain">
111 <label for="url">Url:</label>
112 <input type="url" name="url" id="url" value="" />
115 <div data-role="fieldcontain">
116 <label for="tel">Tel:</label>
117 <input type="tel" name="tel" id="tel" value="" />
120 <div data-role="fieldcontain">
121 <label for="time">Time:</label>
122 <input type="time" name="time" id="time" value="" />
125 <div data-role="fieldcontain">
126 <label for="date">Date:</label>
127 <input type="date" name="date" id="date" value="" />
130 <div data-role="fieldcontain">
131 <label for="month">Month:</label>
132 <input type="month" name="month" id="month" value="" />
135 <div data-role="fieldcontain">
136 <label for="week">Week:</label>
137 <input type="week" name="week" id="week" value="" />
140 <div data-role="fieldcontain">
141 <label for="datetime">Datetime:</label>
142 <input type="datetime" name="datetime" id="datetime" value="" />
145 <div data-role="fieldcontain">
146 <label for="datetime-l">Datetime local:</label>
147 <input type="datetime-local" name="datetime-l" id="datetime-l" value="" />
150 <div data-role="fieldcontain">
151 <label for="color">Color:</label>
152 <input type="color" name="color" id="color" value="" />
158 <p>For multi-line text inputs, use a <code>textarea</code> element. The framework will auto-grow the height of the textarea to avoid the need for an internal scrollbar. </p>
159 <p>Set the <code>for</code> attribute of the <code>label</code> to match the ID of the <code>textarea</code> so they are semantically associated, and wrap them in a <code>div</code> with the <code> data-role="fieldcontain"</code> attribute to group them.</p>
162 <strong><label for="textarea-a">Textarea:</label>
163 <textarea name="textarea" id="textarea-a"></strong>
164 I'm a basic textarea. If this is pre-populated with content, the height will be automatically adjusted to fit without needing to scroll. That is a pretty handy usability feature.
165 <strong></textarea></strong>
168 <p>This will produce a basic textarea with the width set to 100% of the parent container and the label stacked on a separate line. The textarea will grow to fit new lines as you type:</p>
169 <label for="textarea-a">Textarea:</label>
170 <textarea name="textarea" id="textarea-a">
171 I'm a basic textarea. If this is pre-populated with content, the height will be automatically adjusted to fit without needing to scroll. That is a pretty handy usability feature.
175 <strong><div data-role="fieldcontain">
176 </strong><label for="textarea">Textarea:</label>
177 <textarea name="textarea" id="textarea"></textarea>
178 <strong></div></strong>
181 <p>The textarea is displayed like this and will grow to fit new lines as you type:</p>
182 <div data-role="fieldcontain">
183 <label for="textarea">Textarea:</label>
184 <textarea name="textarea" id="textarea"></textarea>
188 <h2>Calling the textinput plugin</h2>
190 <p>This plugin will auto initialize on any page that contains a textarea or any of the text input types listed above without any need for a <code>data-role</code> attribute in the markup. However, if needed, you can directly call the <code>textinput</code> plugin on any selector, just like any jQuery plugin:</p>
192 $('input').textinput();
196 <h2>Degraded input types</h2>
197 <p>jQuery Mobile degrades several HTML5 input types back to type=text or type=number after adding enhanced controls. For example, inputs with a type of range are enhanced with a custom slider control, and their type is set to number to offer a usable form input alongside that slider. Inputs with a type of search are degraded back to type=text after we add our own themable search input styling.</p>
198 <p>The page plugin contains a list of input types that are set to either true which means they'll degrade to type=text, false which means they'll be left alone, or a string such as "number", which means they'll be converted to that type (such as the case of type=range).</p>
200 <p>You can configure which types are changed via the page plugin's <code>degradeInputs</code> option, which can be manipulated externally via <code>$.mobile.page.prototype.options.degradeInputs</code>, which has properties: color, date, datetime, "datetime-local", email, month, number, range, search, tel, time, url, and week. Be sure to configure this inside an event handler bound to the <code>mobileinit</code> event, so that it applies to the first page as well as subsequent pages that are loaded.</p>
205 </div><!--/content-primary -->
207 <div class="content-secondary">
209 <div data-role="collapsible" data-collapsed="true" data-theme="b" data-content-theme="d">
211 <h3>More in this section</h3>
213 <ul data-role="listview" data-theme="c" data-dividertheme="d">
215 <li data-role="list-divider">Form elements</li>
216 <li><a href="../docs-forms.html">Form basics</a></li>
217 <li><a href="../forms-all.html">Form element gallery</a></li>
218 <li><a href="../forms-all-mini.html">Mini form element gallery</a></li>
219 <li data-theme="a"><a href="index.html">Text inputs</a></li>
220 <li><a href="../search/">Search input</a></li>
221 <li><a href="../slider/">Slider</a></li>
222 <li><a href="../switch/">Flip toggle switch</a></li>
223 <li><a href="../radiobuttons/">Radio buttons</a></li>
224 <li><a href="../checkboxes/">Checkboxes</a></li>
225 <li><a href="../selects/">Select menus</a></li>
226 <li><a href="../forms-themes.html">Theming forms</a></li>
227 <li><a href="../forms-all-native.html">Native form elements</a></li>
228 <li><a href="../forms-sample.html">Submitting forms</a></li>
235 </div><!-- /content -->
237 <div data-role="footer" class="footer-docs" data-theme="c">
238 <p>© 2011-12 The jQuery Foundation</p>