Why isn't textarea an input[type="textarea"]?
Solution 1
Maybe this is going a bit too far back but…
Also, I’d like to suggest that multiline text fields have a different type (e.g. “textarea") than single-line fields ("text"), as they really are different types of things, and imply different issues (semantics) for client-side handling.
– Marc Andreessen, 11 October 1993
Solution 2
So that its value can easily contain quotes and <> characters and respect whitespace and newlines.
The following HTML code successfully pass the w3c validator and displays <,> and & without the need to encode them. It also respects the white spaces.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Yes I can</title>
</head>
<body>
<textarea name="test">
I can put < and > and & signs in
my textarea without any problems.
</textarea>
</body>
</html>
Solution 3
A textarea
can contain multiple lines of text, so one wouldn't be able to pre-populate it using a value
attribute.
Similarly, the select
element needs to be its own element to accommodate option
sub-elements.
Solution 4
It was a limitation of the technology at the time it was created. My answer copied over from Programmers.SE:
From one of the original HTML drafts:
NOTE: In the initial design for forms, multi-line text fields were supported by the Input element with TYPE=TEXT. Unfortunately, this causes problems for fields with long text values. SGML's default (Reference Quantity Set) limits the length of attribute literals to only 240 characters. The HTML 2.0 SGML declaration increases the limit to 1024 characters.
Solution 5
I realize this is an older post, but thought this might be helpful to anyone wondering the same question:
While the previous answers are no doubt valid, there is a more simple reason for the distinction between textarea and input.
As mentioned previously, HTML is used to describe and give as much semantic structure to web content as possible, including input forms. A textarea may be used for input, however a textarea can also be marked as read only via the readonly attribute. The existence of such an attribute would not make any sense for an input type, and thus the distinction.
Related videos on Youtube
Comments
-
Diogo Cardoso almost 4 years
Why is there an element
<textarea>
instead of<input type="textarea">
?-
BalusC almost 13 yearsThere's by the way also an
<select>
instead of<input type="select">
. The<input>
just represents a basic input element. Thetype
attribute just represents the type of the value it holds.
-
-
k to the z about 13 yearsI'd prefer a w3c origin myth.
-
Quentin about 13 yearsTextarea elements are not defined as containing CDATA, you still need to use entities for
<
,&
, etc. It is just so it can handle whitespace. -
Guillaume Esquevin about 13 yearsI just tested it and yes, you can put unencoded <, > and & within a textarea. And it successfully pass the w3c validator.
-
Marcel almost 13 years@ktothez: See my answer.
-
Serhiy about 12 yearsYes, "different type", couldn't the same have been achieved via <input type="textarea"> blah blah \n \n blah </input> ? Why a distinct tag?
-
Guillaume Esquevin over 11 yearsThat's definitely possible :/ I think @Marcel has the proper answer.
-
JohnK about 11 yearsThere's not one proper answer here. As with life in general (i.e., outside a box) there are multiple reasons for something being the way it is.
-
bart almost 11 years@Serhiy I agree, it doesn't make much sense to introduce a new element for that (just like password doesn't have its own element). Unfortunately W3C isn't always consistent.
-
Mark Cidade about 10 yearsw3c is pretty consistent. this was before w3c
-
Foreever about 10 yearsI wonder how this got this much upvote. The question is not about the difference between 'text' and 'textarea', but the reason for including the multi-line text as <textarea> tag rather than as a 'type=textarea' attribute in <input> tag.
-
Matt almost 10 yearsThis sounds reasonable, except that
input[type="text"]
can take the readonly attribute too. Which is sort of odd, now that you point it out! w3.org/TR/html-markup/input.text.html#input.text.attrs.readonly -
OJFord over 9 yearsWhy wouldn't one be able to populate it with the
value
attr? Overflow wraps to the next line on resize of thetextarea
anyway. -
Mark Cidade over 9 yearsyou can't use line breaks in attributes
-
Marcel over 9 years@Foreever this is about as direct an answer as it gets. The reason there is a
textarea
element is because Marc Andreessen proposed it back in October 1993 for the reasons as quoted above. -
Piotr Dobrogost over 8 years@Marcel Marc Andreessen suggested that there should be a different type not tag. There are different types of input denoted by different values of
type
attribute ofinput
tag and they all share one and the sameinput
tag. So no, this quote is not an answer to this question. -
Matthew over 8 yearswell clearly if you put </textarea> in there it wouldn't work so yes, you need to escape everything inside the
textarea
element anyway. -
Marcel over 7 years@PiotrDobrogost here's another post later that same day where he says "multiline texts fields should have a different name". It appears terminology wasn't being used as precisely as you'd think at this point in time so it isn't easy to tell either way whether he is referring to a new tag or type value. My previous comment also still stands.
-
usr about 7 yearsI think this answer should call out the obvious: It was a mistake. His reasoning makes no sense.
-
Mariusz Jamro over 6 yearsSo checkbox is the same as text input, but single-line text input is something totally different than multi-line one...