automatically resizing textarea (IE problem)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arashamiri@hotmail.com

    automatically resizing textarea (IE problem)

    Hi, I wrote an auto resizing textarea:



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


    function resizeTextArea( t,minCols,minRo ws) {
    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(textL ine[i].length) >
    parseInt(t.getA ttribute("cols" ))) {
    lines += Math.floor(pars eInt(textLine[i].length) /
    parseInt(t.getA ttribute("cols" ))) ;
    }
    }
    t.setAttribute( "rows",myMax(li nes+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

  • arashamiri@hotmail.com

    #2
    Re: automatically resizing textarea (IE problem)

    btw.
    you call it like this

    <textarea onkeypress="res izeTextArea(thi s,10,2)">
    here is some text....
    </textarea>

    Comment

    • gabru

      #3
      Re: automatically resizing textarea (IE problem)

      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.overflo w = "auto";

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

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

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

      arashamiri@hotm ail.com wrote:[color=blue]
      > Hi, I wrote an auto resizing textarea:
      >
      >
      >
      > function myMax(anumber, another) {
      > if (anumber > another) {
      > return anumber;
      > }
      > return another;
      > }
      >
      >
      > function resizeTextArea( t,minCols,minRo ws) {
      > 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 &lt; textLine.length ;i++) {
      > if (parseInt(textL ine[i].length) &gt;
      > parseInt(t.getA ttribute("cols" ))) {
      > lines += Math.floor(pars eInt(textLine[i].length) /
      > parseInt(t.getA ttribute("cols" ))) ;
      > }
      > }
      > t.setAttribute( "rows",myMax(li nes+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[/color]

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: automatically resizing textarea (IE problem)

        gabru wrote:
        [color=blue]
        > <html>
        > <head></head>
        >
        > <body>
        > <script>[/color]

        Not at all Valid HTML. <URL:http://validator.w3.or g/>
        [color=blue]
        > function getMax(anumber, another) {
        > return((anumber > another) ? anumber : another);
        > }[/color]

        Math.max() exists since the very first versions of JavaScript
        and JScript, and it was specified in ECMAScript Edition 1.
        [color=blue]
        > [top post][/color]

        <URL:http://jibbering.com/faq/faq_notes/pots1.html>


        PointedEars

        Comment

        Working...