I have a textarea with white-space: wrap;
.
However, when the textarea is disabled, the line breaks are ignored:
const textarea = document.querySelector("textarea");
document.querySelector("button").addEventListener("click", (e) => {
if (!textarea.hasAttribute("disabled")) {
textarea.setAttribute("disabled", "disabled");
} else {
textarea.removeAttribute("disabled", "disabled");
}
});
textarea,
textarea:disabled {
width: 600px;
height: 100px;
white-space: wrap;
}
<textarea wrap="on">1asdfasdf
2afdasdfafdasdfafdasdfaw
3asdfasdf
4asdfasdf</textarea>
<br/>
<button>Toggle 'disabled'</button>
I want the textarea content to stay the same, no matter disabled or not. It also includes textarea’s scrollHeight.
Is it possible?
2
Answers
The
wrap
value is not valid for thewhite-space
CSS property, and the accepted values can be one of the following:To fix this, you can use
white-space: pre-wrap;
.This will ensure that the content of the
textarea
is wrapped at the end of lines and that white spaces and line breaks are preserved, even when thetextarea
is disabled.The final version should be like this:
pre
vspre-wrap
vspre-line
white-space: pre;
n
) in the HTML source or at<br>
elements. It does not automatically wrap text to the next line when it exceeds the container’s width.white-space: pre-wrap;
pre
, it preserves all sequences of white space.<br>
elements, and automatically break/wrap when they reach the end of the container (line boxes).white-space: pre-line;
pre
andpre-wrap
,pre-line
collapses sequences of white space into a single white space. However, it still preserves line breaks in the content.<br>
elements, and text wraps at the edge of the container.You need
white-space: pre-line