The size
property sets or gets the size attribute of a text field.
The size
attribute specifies the width of a text field in number of characters.
The default value is 20.
The size property is supported in all major browsers.
size |
Yes | Yes | Yes | Yes | Yes |
Return the size property.
var v = textObject.size
Set the size property.
textObject.size=number
Value | Description |
---|---|
number | Set the width of the text field in number of characters. Default value is 20 |
A Number type value representing the width of the text field in number of characters.
The following code shows how to get the width of a text field.
<!DOCTYPE html>
<html>
<body>
<!--from w w w . j av a 2 s . c om-->
Name: <input type="text" id="myText" size="30">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myText").size;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to set the width of a text field.
<!DOCTYPE html>
<html>
<body>
<!--from ww w . j a va 2 s. com-->
Name: <input type="text" id="myText">
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
document.getElementById("myText").size = "50";
}
</script>
</body>
</html>
The code above is rendered as follows: