Javascript Reference - HTML DOM Textarea cols Property








The cols property sets or gets the cols attribute of a textarea.

The cols attribute sets the visible width of a textarea in characters.

Browser Support

cols Yes Yes Yes Yes Yes

Syntax

Return the cols property.

var v = textareaObject.cols 

Set the cols property.

textareaObject.cols=number




Property Values

Value Description
number Set the width of the textarea in number of characters. Default is 20

Return Value

A Number type value representing the width of the textarea in characters.

Example

The following code shows how to change the width of a textarea.


<!DOCTYPE html>
<html>
<body>
<textarea id="myTextarea" rows="4" cols="50">
</textarea>
<button type="button" onclick="myFunction()">test</button>
<script>
function myFunction() {<!--from   w w w  .  j a v a  2  s  .  c  om-->
    document.getElementById("myTextarea").cols = "100";
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the width of the textarea in characters.


<!DOCTYPE html>
<html>
<body>
<textarea id="myTextarea" rows="4" cols="50">
</textarea>
<button type="button" onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--  w ww.  j  ava  2  s  . c  o  m-->
    var x = document.getElementById("myTextarea").cols;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:

Example 3

The following code shows how to change the width of a textarea using the style.width property.


<!DOCTYPE html>
<html>
<body>
<!--  www.j  a  v a2s .c o  m-->
<textarea id="myTextarea">
</textarea>
<button type="button" onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myTextarea").style.width = "300px";
}
</script>

</body>
</html>

The code above is rendered as follows: