Why isn't textarea an input[type="textarea"]?

117,867

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.

Share:
117,867

Related videos on Youtube

Diogo Cardoso
Author by

Diogo Cardoso

JavaScript enthusiast

Updated on June 27, 2020

Comments

  • Diogo Cardoso
    Diogo Cardoso almost 4 years

    Why is there an element <textarea> instead of <input type="textarea">?

    • BalusC
      BalusC almost 13 years
      There's by the way also an <select> instead of <input type="select">. The <input> just represents a basic input element. The type attribute just represents the type of the value it holds.
  • k to the z
    k to the z about 13 years
    I'd prefer a w3c origin myth.
  • Quentin
    Quentin about 13 years
    Textarea 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
    Guillaume Esquevin about 13 years
    I just tested it and yes, you can put unencoded <, > and & within a textarea. And it successfully pass the w3c validator.
  • Marcel
    Marcel almost 13 years
    @ktothez: See my answer.
  • Serhiy
    Serhiy about 12 years
    Yes, "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
    Guillaume Esquevin over 11 years
    That's definitely possible :/ I think @Marcel has the proper answer.
  • JohnK
    JohnK about 11 years
    There'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
    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
    Mark Cidade about 10 years
    w3c is pretty consistent. this was before w3c
  • Foreever
    Foreever about 10 years
    I 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
    Matt almost 10 years
    This 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.reado‌​nly
  • OJFord
    OJFord over 9 years
    Why wouldn't one be able to populate it with the value attr? Overflow wraps to the next line on resize of the textarea anyway.
  • Mark Cidade
    Mark Cidade over 9 years
    you can't use line breaks in attributes
  • Marcel
    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
    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 of input tag and they all share one and the same input tag. So no, this quote is not an answer to this question.
  • Matthew
    Matthew over 8 years
    well clearly if you put </textarea> in there it wouldn't work so yes, you need to escape everything inside the textarea element anyway.
  • Marcel
    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
    usr about 7 years
    I think this answer should call out the obvious: It was a mistake. His reasoning makes no sense.
  • Mariusz Jamro
    Mariusz Jamro over 6 years
    So checkbox is the same as text input, but single-line text input is something totally different than multi-line one...