Javascript Reference - HTML DOM Textarea maxLength Property








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.

Browser Support

maxLength Yes 10.0 Yes Yes Yes

Syntax

Return the maxLength property.

var v = textareaObject.maxLength 

Set the maxLength property.

textareaObject.maxLength=number




Property Values

Value Description
number Set the maximum number of characters allowed in the textarea

Return Value

A Number type value representing the maximum number of characters allowed in the textarea.

Example

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:





Example 2

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: