HTML DOM
The maxLength property of the HTMLTextAreaElement interface indicates the maximum number of characters (in UTF-16) allowed to be entered for the value of the textarea element, and the maximum number of characters allowed for the value to be valid. It reflects the element's maxlength attribute. -1 means there is no limit on the length of the value.
[!NOTE] Browsers generally prevent users from entering more characters than the
maxlengthattribute allows. Should the length be longer, the element is considered invalid and the ValidityState object's ValidityState.tooLong property will betrue.
Value
A number representing the element's maxlength if present, or -1.
Example
Given the following HTML:
<p>
<label for="comment">Comment</label>
<textarea id="comment" minlength="10" maxlength="200"></textarea>
</p>
You can use the maxLength property to retrieve or set the <textarea>'s maxlength attribute value:
const textareaElement = document.querySelector("#comment");
console.log(`Element's maxLength: ${textareaElement.maxLength}`); // "Element's maxlength: 200"
textareaElement.maxLength = 220; // updates the element's maxlength attribute value
Specifications
Browser compatibility
See also
- HTMLTextAreaElement.value
- HTMLTextAreaElement.minLength
- ValidityState.tooLong
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
