Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is omitting type="text" on an input considered bad practice?

Tags:

html

I have an HTML page that is way too junky, so I am trying to trim it down.

I always put type='text' on my inputs, when they are text inputs:

<input type="text" />

However, it doesn't seem that browsers strictly require that.

Is omitting this attribute where it is text considered bad practice?

like image 327
dthree Avatar asked Dec 20 '14 04:12

dthree


3 Answers

The type attribute has always been optional, defaulting to text, in all HTML specifications (which start from HTML 2.0) and in all implementations. It is thus safe to omit it, and of course equally safe to include it, when you want to have a text input field.

In the DOM, <input> and <input type=text>, have the same representation (including the presence of a type property of the element node, with the value 'text'), except that only in the latter case the attributes property contains type. So any JavaScript processing that needs the type property works just the same.

It is a matter of opinion and taste whether it is useful to include it for symmetry with other input elements or for explicitness, so that a person reading HTML source will immediately see that we have a text input field, without needing to scan a possibly long list of attributes to see that no type attribute is present.

However, there is a technical difference that may matter, possibly making it advantageous to use the type=text attribute. In CSS, the selector input[type=text] matches only elements that have the attribute type=text explicitly set. If such attributes are used consistently, you can thus set styling properties for text input fields without affecting any other input fields. Otherwise you need a more complicated selector.

like image 134
Jukka K. Korpela Avatar answered Oct 16 '22 19:10

Jukka K. Korpela


To answer to this question we can refer to:

HTML4 SPEC 17.4 The INPUT element

type = text|password|checkbox|radio|submit|reset|file|hidden|image|button [CI] This attribute specifies the type of control to create. The default value for this attribute is "text".

The HTML5 Spec 4.10.5 The input element

The type attribute controls the data type (and associated control) of the element. It is an enumerated attribute. The missing value default is the Text state.

So, omitting the attribute is not considerable a bad practice

like image 40
alessandro Avatar answered Oct 16 '22 21:10

alessandro


text is the default value for an input tag's type attribute in most if not all modern browsers. However, it isn't considered a "bad practice" to leave it off, only /good/ practice to include it.

like image 1
Coding Orange Avatar answered Oct 16 '22 20:10

Coding Orange