HTMLTextAreaElement: maxLength プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

maxLengthHTMLTextAreaElement インターフェイスのプロパティで、この <textarea> 要素の値として入力が許可される最大文字数(UTF-16 コード単位)と、有効な値として許可される最大文字数を示します。これは、この要素の maxlength 属性を反映します。-1 は、値の長さに制限がないことを意味します。

メモ: ブラウザーは通常、maxlength 属性で許可された文字数以上の文字が入力できないようにしています。長さがこれより長い場合、その要素は無効と見なされ、ValidityState オブジェクトの tooLong プロパティが true になります。

存在する場合は要素の maxlength を表す数値、さもなくば -1

次の HTML があったとします。

html
<p>
  <label for="comment">コメント</label>
  <textarea id="comment" minlength="10" maxlength="200" /></textarea>
</p>

maxLength プロパティを使用して、<textarea>maxlength 属性値を取得または設定することができます。

js
const textareaElement = document.querySelector("#comment");
console.log(`要素の maxLength: ${textareaElement.maxLength}`); // "要素の maxlength: 200"
textareaElement.maxLength = 220; // この要素の maxlength 属性値を更新

仕様書

Specification
HTML
# dom-textarea-maxlength

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
maxLength

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報