The maxLength
property sets or gets the maxlength attribute of a textarea.
The maxLength
attribute sets the maximum number of characters allowed in a textarea.
maxLength |
Yes | 10.0 | Yes | Yes | Yes |
Return the maxLength property.
var v = textareaObject.maxLength
Set the maxLength property.
textareaObject.maxLength=number
Value | Description |
---|---|
number | Set the maximum number of characters allowed in the textarea |
A Number type value representing the maximum number of characters allowed in the textarea.
The following code shows how to get the maximum number of characters allowed in a textarea.
<!DOCTYPE html>
<html>
<body>
<!--from w w w.ja v a2s . c om-->
Address:<br>
<textarea id="myTextarea" maxlength="30">
this is a test
</textarea>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myTextarea").maxLength;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to change the maximum number of characters allowed in a textarea.
<!DOCTYPE html>
<html>
<body>
<!-- w w w .j av a2 s . c o m-->
<textarea id="myTextarea">
</textarea>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
document.getElementById("myTextarea").maxLength = "4";
}
</script>
</body>
</html>
The code above is rendered as follows: