JavaScript Traps and Pitfalls: Three Normalizations of <textarea> Element's Value
The textarea element represents a multiline plain text edit control for the element's raw value.
For historical reasons, the element's value is normalized in three different ways for three different purposes.
-
The raw value is the value as it was originally set. It is not normalized.
-
The API value is the value used in the
value
IDL attribute,textLength
IDL attribute, and by themaxlength
andminlength
content attributes. It is normalized so that line breaks use U+000A LINE FEED (LF) characters. -
Finally, there is the value, as used in form submission and other processing models in this specification. It is normalized so that line breaks use U+000D CARRIAGE RETURN U+000A LINE FEED (CRLF) character pairs, and in addition, if necessary given the element's
wrap
attribute, additional line breaks are inserted to wrap the text at the given width.
All info above is copied from HTML Standard.
So for eaxmple,
-
you get the API value, when using
var el = document.querySelector('textarea'); console.log(el.value);
in JavaScript, which will never contain\r
in it; -
value is submitted to server when posting a form, using
\r\n
as the line break, which is consistent with what's used in an HTTP message; -
and there is no way to retrieve the raw value.