automatically resizing textarea (IE problem)

A

arashamiri

Hi, I wrote an auto resizing textarea:



function myMax(anumber, another) {
if (anumber > another) {
return anumber;
}
return another;
}


function resizeTextArea(t,minCols,minRows) {
var lines = 0;
if (t.getAttribute("cols") == null) t.setAttribute("cols",minCols);
if (t.getAttribute("rows") == null) t.setAttribute("rows",minRows);
textLine = t.value.split("\n");

// get lines of textbox
lines = t.value.split("\n").length;

// get longest row of textbox.
var max = 0;

for (i=0;i < textLine.length;i++) {
if (parseInt(textLine.length) >
parseInt(t.getAttribute("cols"))) {
lines += Math.floor(parseInt(textLine.length) /
parseInt(t.getAttribute("cols"))) ;
}
}
t.setAttribute("rows",myMax(lines+1,minRows));
}

In Firefox this works really fine!

But in IE the text area grows very strange. If I add a line, after a
while there seems to be a problem with spacing.

Anybody has some ideas?
arash
 
A

arashamiri

btw.
you call it like this

<textarea onkeypress="resizeTextArea(this,10,2)">
here is some text....
</textarea>
 
G

gabru

hi arash,
check this out:

<html>
<head></head>

<body>
<script>
function getMax(anumber, another) {
return((anumber > another) ? anumber : another);
}

/*****************************************************************************
* @SDESCRIPTION: automatically resizes a textarea depending on the
input
* @DESCRIPTION: call the function in the onkeyup-event of the tarea.
* @PARAM: t [textarea]: the textarea you want to handle
* @PARAM: minRows [int]: minimum amount of rows
* @PARAM: minCols [int], OPTIONAL: minimum amount of columns.
*****************************************************************************/
function resizeTextArea(t, minRows, minCols) {
t.rows = minRows;
t.setAttribute("wrap", "off");
t.style.overflow = "auto";

lines = t.value.split("\n");

if (arguments.length > 2) {
t.cols = minCols;
maxChars = lines[0].length;
for(i = 1; i < lines.length; i++) {
currentLength = lines.length;
if (currentLength > maxChars) maxChars = currentLength;
}
t.cols = getMax(maxChars, minCols);
}
t.rows = getMax(lines.length + 1, minRows);
}
</script>

<textarea onkeyup="resizeTextArea(this, 10);"></textarea>
<textarea onkeyup="resizeTextArea(this, 10, 5);"></textarea>
</body>
</html>
 

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,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top