type (HTML attribute)
Example
The type is
set to "text" for this
input:
<input type="text" name="txtsearch" id="txtsearch" size="20" />
Description
Of all the attributes that can
be applied to an input, the type
attribute is the one that has the biggest impact. With a change of this
value, the appearance and behaviour of the input can
change dramatically, from a simple text input, a masked password field, to
controls such as radio buttons or checkboxes and even images. The various
features and usage are detailed in the list below:
"checkbox"- this type of input is used when the use needs to answer a yes/no type question. There is no way the user can enter any data of their own, it is simply a case of checking or unchecking the box. When theformis submitted, the server will be interested in thevalueattribute attached to the control and the state of the checkbox (checked or unchecked). With a checkboxinput, the control should come first followed by the associated text, e.g.<input type="checkbox" name="chknewsletter" id="chknewsletter" value="newsletter-opt-in" /><label for="chknewsletter">Send me the monthly newsletter</label>
"file"- used for file uploads. When the attribute is set to"file", two controls appear in the browser - a field that looks similar to a text input and a button control which is labelled ‘Browse’. The text that appears on this button cannot be changed and a file upload control is generally difficult to style with CSS. The file upload control has an optional attributeacceptthat is used to define what kinds of files may be uploaded.<input type="file" name="picture" id="picture" accept="image/jpeg, image/gif" />
"image"- the imageinputtype is used in place of a standard submit button which may be a bit boring and conservative for your form design (or perhaps your marketing department are demanding something a little more ‘on-brand’). When using aninputof type"image", you will also need to specify asrc(where the image can be found) and analtattribute (alternative text for the image in case it is missing or cannot be viewed for some other reason).<input type="image" src="submit.jpg" alt="Submit your details" />
"password"- The password field is almost identical to the standard text input.<input type="password" name="pin” id="pin" maxlength="4" size="6" />
The password input has exactly the same set of attributes as the text input, but differs visually in that characters entered in this type of field are masked in the browser, such that entered characters appear as asterisks or blobs, as shown in #type/fig-masked-password."radio"- The"radio"button control is arguably the most complex type ofinputcontrol. To explain how this control works, it’s worth going back to the origin of its name: in old-fashioned radio sets, tuning between stations was not performed by pressing a ‘scan’ button or even rotating a dial, rather a series of buttons that were linked to a handful of radio stations could be chosen, but only one at a time; pressing one button in the range would cause any other button to pop out. The same effect is in play here - only one control can be selected from a range and if you select another, the previously selected input is deselected. For this to work, though, the range of radio buttons that you want to flip between must all have the samenameattribute, as shown below (using an example of picking a radio station, how appropriate!). With a radioinput, the control should come first followed by the associated text, e.g:<div><input type="radio" name="station" id="rad1" value="Radio 1" /> <label for="rad1">Radio 1</label></div> <div><input type="radio" name="station" id="rad2" value="Radio 2" /> <label for="rad2">Radio 2</label></div> <div><input type="radio" name="station" id="rad3" value="XFM" /> <label for="rad3">XFM</label></div> <div><input type="radio" name="station" id="rad4" value="4Music" /> <label for="rad4">4Music</label></div>
Because each control has the samename(in this case"station"), only one can be picked from the list. Thevaluefor each control differs though, so in the example above, if the user chose"XFM"as their favourite radio station, that would be thevalueassociated with the field name of"station". Also note that unlike all the otherformmarkup examples, for this type of control thenameandidvalues differ. Theidis used for the purposes of linking the control to itslabeltext, and must be unique."reset"- The"reset"input is similar to the"button"and"submit"types, at least it is visually - a button control that can be clicked/pressed, and no data is entered by the user. However, aresetcontrol is a very destructive control to introduce to a web page, as pressing it will clear all data already entered in theformthat theresetbutton resides in, usually causing a fair amount of expletives to follow. While it may be perfectly valid HTML to use this control, it is used less and less these days as it is all too easy to accidentally action one of these buttons, either by a mouse click or while tabbing through with the keyboard. If you do feel the presence of a"reset"is warranted, it would be advisable to add some JavaScript in too, something that is able to double check with an “Are you sure you want to clear all data and start again?” type message. However, it is probably best not used at all (the user can easily refresh/reload a page to start again, if they so wish).<input type="reset" name="cmdReset” id="cmdReset" value="Clear form data" />
"submit"- This is the ‘magic button’, the one that you press to send all the form data that you have entered. Once clicked, there’s no going back! The"submit"inputappears with the text that you specify in thevalueattribute, but if novalueis specified, it will simply show on the button face as ‘Submit’.<input type="submit" name="cmdSubmit” id="cmdSubmit" value="Send this application" />
"text"- The most common kind ofinputthat you will encounter on the web (and no doubt need when building your own forms). The"text"inputcreates a single-line box that the user can enter characters into, and has a number of attributes, such assize,maxlength,disabled,readonly,nameandtabindex.
Value
"button",
"checkbox", "file",
"hidden", "image",
"password", "radio",
"reset", "submit" or
"text" only
Compatibility
| IE | 5.5 | Full |
|---|---|---|
| 6.0 | Full | |
| 7.0 | Full | |
| Firefox | 1.0 | Full |
| 1.5 | Full | |
| 2.0 | Full | |
| Safari | 1.3 | Full |
| 2.0 | Full | |
| 3.0 | Full | |
| Opera | 9.2 | Full |
| 9.5 | Full |
Causes no compatibility issues. It has excellent support across all tested browsers.
User-contributed notes
There are no comments yet.
Add a note
To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

