Autosizing textbox

T

Tirrell Cotton

Doe sanyone know how I can make a multiline text box that grows vertically
as more text is entered (basically no scroll bars)?
 
P

PopeDarren

I threw this together haphazardly for IE 6. I didn't test it on other
browsers, but you could use it as a starting point if you don't mind
using javascript.

The script.
    <script type="text/javascript">
//this is the number I got when you only have one line in the
textArea
    var height = 20;
    function getHeight(){
var txtArea = document.getElementById("textArea");

//debugging purposes only
document.getElementById("txt").value =
txtArea.scrollHeight;

//if the scrollHeight is bigger than what we started with,
then add to the size of the box
if (txtArea.scrollHeight > height) {
txtArea.rows=txtArea.rows + 1;
height = txtArea.scrollHeight;
}
}
    </script>

And here are the form elements. The textbox was only for debugging
purposes.
  <input type="text" id="txt" />
    <textarea id="textArea" rows="2" cols="45"
onkeydown="getHeight();"></textarea>

This assumes that you're starting with an empty textarea, so you'll
have to add to it a bit if the textArea is full. Hope it helped!
 
T

Tirrell Cotton

That worked ( after a little tweaking for server side controls). I also
managed to get the textbox to shrink if text is removed. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,177
Latest member
OrderGlucea
Top