...

View Full Version : Automatic textarea resize



meediake
05-29-2006, 02:51 PM
Hello everybody,
I try to make a function, what resizes my textarea. Everything is OK, but it is not resizing normally. I have a problem writing my function to count lines in textarea(also these lines, which are wrapped).
Does anybody have an idea or allready a correctly functioning function to count lines?
My own function looks like this:


function count_lines(strtocount, cols) {
var hard_lines = 1;
var last = 0;
while (true) {
last = strtocount.indexOf("\n", last+1);
hard_lines ++;
if ( last == -1 ) break;
}
var soft_lines = Math.round(strtocount.length / (cols-1));
var hard = eval('hard_lines ' + unescape('%3e') + 'soft_lines;');
if (hard) soft_lines = hard_lines;
return soft_lines;
}


thank you !

vwphillips
05-29-2006, 05:43 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
</head>

<body>
<textarea id="Tom" onkeyup="Height();" style="width:200px;font-size:12px;font-family:Courier New;" >
1 Some Text
</textarea>

<div id="fred" style="position:absolute;visibility:hidden;font-size:12px;font-family:Courier New;border:solid black 1px;" >
Dummy Div
</div>
<script language="JavaScript" type="text/javascript">
<!--

function Height(){
var ta=document.getElementById('Tom');
var div=document.getElementById('fred');
div.style.width=ta.offsetWidth+'px';
div.innerHTML=ta.value;
ta.style.height=(div.offsetHeight+RegExpTest(ta.value)*12)+'px';
}

function RegExpTest(txt){
var st=1
var re=/\n+/g;
var arr;
while ((arr = re.exec(txt))!=null) { st++; }
return st
}

//-->
</script>



</body>

</html>

vwphillips
05-29-2006, 09:32 PM
see

http://www.vicsjavascripts.org.uk/FormCompendium/FormCompendium.htm#f66

mrhoo
05-30-2006, 04:42 AM
I split the textarea.value string on newlines and
then loop through them, adding the (rounded-up) result of
dividing the length of each line by the width (cols attribute).

It catches the wrapped lines....

meediake
05-30-2006, 08:47 AM
thank you guys for your snippets... but still I have the same problem, these scripts also won't loose the scrollbar from textarea.. when I paste some tekst from lipsum.com, still the scrollbar appears:(

vwphillips
05-30-2006, 09:08 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
</head>

<body>
<input type="button" value="Big" id="fred" onclick="SwapImage('fred','Small');" >
<div style="position:relative;overflow:hidden;width:180px;border-right:solid blue 1px;" >
<textarea rows=4 style="width:200px;" border=0 >
1
</textarea>
</div>
</body>

</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum