I have set resize to vertical but I would like that when the user fills the textbox then its size expands down. Is there any way this can be done without using an external library like jQuery?
Asked
Active
Viewed 7,996 times
1
-
2Possible duplicate of [dynamically increase input type text textbox width according to the characters entering into it](http://stackoverflow.com/questions/20727692/dynamically-increase-input-type-text-textbox-width-according-to-the-characters-e) – Gaurav Aggarwal Aug 03 '16 at 08:34
-
Almost. but here OP wants it vertically expanded – jonju Aug 03 '16 at 08:37
-
Javascript is allowed? – tewathia Aug 03 '16 at 08:39
-
Please add a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) of your code – Jamie Barker Aug 03 '16 at 08:40
-
1Possible duplicate of [Creating a textarea with auto-resize](http://stackoverflow.com/questions/454202/creating-a-textarea-with-auto-resize) – Martin Braun Aug 03 '16 at 08:48
2 Answers
1
This has been answered here already: Creating a textarea with auto-resize
<!DOCTYPE html>
<html>
<head>
<title>autoresizing textarea</title>
<style type="text/css">
textarea {
border: 0 none white;
overflow: hidden;
padding: 0;
outline: none;
background-color: #D0D0D0;
resize: none;
}
</style>
<script type="text/javascript">
var observe;
if (window.attachEvent) {
observe = function (element, event, handler) {
element.attachEvent('on'+event, handler);
};
}
else {
observe = function (element, event, handler) {
element.addEventListener(event, handler, false);
};
}
function init () {
var text = document.getElementById('text');
function resize () {
text.style.height = 'auto';
text.style.height = text.scrollHeight+'px';
}
/* 0-timeout to get the already changed text */
function delayedResize () {
window.setTimeout(resize, 0);
}
observe(text, 'change', resize);
observe(text, 'cut', delayedResize);
observe(text, 'paste', delayedResize);
observe(text, 'drop', delayedResize);
observe(text, 'keydown', delayedResize);
text.focus();
text.select();
resize();
}
</script>
</head>
<body onload="init();">
<textarea rows="1" style="height:1em;" id="text"></textarea>
</body>
</html>
Example: https://jsfiddle.net/hmelenok/WM6Gq/
Credits go to panzi, vote him up here: https://stackoverflow.com/a/5346855/1540350

Community
- 1
- 1

Martin Braun
- 10,906
- 9
- 64
- 105