CKTIS / JavaScript
Newline normalization in HTMLTextAreaElement.value
Reading `HTMLTextAreaElement.value` returns text with LF line endings, even if the string assigned to it contained CRLF pairs or lone CR characters.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
textarea.value = text;
const normalized = textarea.value;HTMLTextAreaElement.value newline normalization applies when you read the current text of a textarea. Assigning to value sets the control’s raw value; reading value returns the normalized API value. Each CRLF pair (\r\n) and each lone CR (\r) becomes one LF (\n). Existing LF characters stay unchanged.
This is an HTML DOM rule, not a general rule for ECMAScript strings. The assignment leaves the original string unchanged, but reading value cannot tell you which LF characters were originally CRLF or lone CR. Keep the original separately if those differences matter. defaultValue reflects the element’s default text, not its current edited text.
Visual wrapping does not add characters to value. For form submission, wrap="hard" with cols set can add line breaks to the submitted value. The multipart/form-data encoder serializes line endings in a text field’s value as CRLF; that does not change what reading value returns.
Examples
<!doctype html>
<meta charset="utf-8">
<title>Textarea newline normalization</title>
<script>
const textarea = document.createElement("textarea");
const original = "A\r\nB\rC\nD";
textarea.value = original;
console.log(JSON.stringify(original));
console.log(JSON.stringify(textarea.value));
</script>Result
"A\r\nB\rC\nD" "A\nB\nC\nD"
Save the example as an `.html` file, open it in a browser, and check the browser console. `JSON.stringify` makes line endings visible as escapes: the first line shows the original string and the second shows the result of reading `value`. The element does not need to be inserted into the document.
Related lessons
- JavaScript · Lesson 9 · Advanced · Version: 4