Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2003
    Location
    PHP-LAND
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Automatic textarea resize

    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:
    Code:
    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 !

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,535
    Thanks
    3
    Thanked 513 Times in 500 Posts
    Code:
    <!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>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,535
    Thanks
    3
    Thanked 513 Times in 500 Posts
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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....

  • #5
    New Coder
    Join Date
    Nov 2003
    Location
    PHP-LAND
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,535
    Thanks
    3
    Thanked 513 Times in 500 Posts
    Code:
    <!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>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •