Javascript Reference - HTML DOM Input Search size Property








The size property sets or gets the size attribute of a search field.

The size attribute specifies the width of a search field in number of characters. The default value is 20.

Browser Support

size Yes Yes Yes Yes Yes

Syntax

Return the size property.

var v = searchObject.size

Set the size property.

searchObject.size=number




Property Values

Value Description
number Set the width of the search field in number of characters. Default value is 20

Return Value

A Number type value representing the width of the search field, in number of characters.

Example

The following code shows how to get the width of a search field in number of characters.


<!DOCTYPE html>
<html>
<body>
<!--   w w w  . j av  a 2 s . co  m-->
Search: <input type="search" id="mySearch" size="35">
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("mySearch").size;
    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 width of a search field.


<!DOCTYPE html>
<html>
<body>
<!--from www  .  ja va  2  s.c om-->
Search: <input type="search" id="mySearch">
<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    document.getElementById("mySearch").size = "50";
}
</script>

</body>
</html>

The code above is rendered as follows: