[Repost] find textarea number of cols when it is set to 100%

Discussion in 'Javascript' started by nospam, Jul 22, 2004.

  1. nospam

    nospam Guest

    [if this question was already anwsered, simply point me to a link, I
    did not find anything with the keywords of the subject. If it is not
    clear, tell me]

    Hi,
    I have an application whose textareas rows automatically resize onFocus
    to the number of lines in the textArea.
    The cols are set to 100% (in a CSS file) in order to always take the
    full browser width

    I use textarea.value.split('\n') to find the number of lines.
    Some lines are longer than the 100% width and they wrap.
    However, wrappeed lines do not contain '\n'. Thus my textarea has less
    lines than what is needed to display it without scroll bars.

    I don't want to use wrap='hard' as the text shall be stored in the DB in
    its original form (even if lines are too long to be displayed without
    wrapping).

    I would like thus to get the number of cols dynamically
    in order to do:
    lines=textarea.value.split('\n');
    realNumberOfLines=0;
    for(i=0; 1<lines.length; i++) {
    realNumberOfLines = realNumberOfLines +
    Math.ceil(lines.length/textarea.cols);
    }
    return realNumberOfLines;


    The problem is that textare.cols returns -1 in mozilla 1.7 and a value
    lower than the reality in IE6.

    Does anybody have an idea on how to achieve that?

    Regards,
    Arnaud.
    nospam, Jul 22, 2004
    #1
    1. Advertising

  2. nospam

    Guest

    On Thu, 22 Jul 2004 12:55:24 +0200, nospam <>
    wrote:

    >[if this question was already anwsered, simply point me to a link, I
    >did not find anything with the keywords of the subject. If it is not
    >clear, tell me]
    >
    >Hi,
    >I have an application whose textareas rows automatically resize onFocus
    >to the number of lines in the textArea.
    >The cols are set to 100% (in a CSS file) in order to always take the
    >full browser width
    >
    >I use textarea.value.split('\n') to find the number of lines.
    >Some lines are longer than the 100% width and they wrap.
    >However, wrappeed lines do not contain '\n'. Thus my textarea has less
    >lines than what is needed to display it without scroll bars.
    >
    >I don't want to use wrap='hard' as the text shall be stored in the DB in
    >its original form (even if lines are too long to be displayed without
    >wrapping).
    >
    >I would like thus to get the number of cols dynamically
    >in order to do:
    > lines=textarea.value.split('\n');
    > realNumberOfLines=0;
    > for(i=0; 1<lines.length; i++) {
    > realNumberOfLines = realNumberOfLines +
    > Math.ceil(lines.length/textarea.cols);
    > }
    > return realNumberOfLines;
    >
    >
    >The problem is that textare.cols returns -1 in mozilla 1.7 and a value
    >lower than the reality in IE6.
    >
    >Does anybody have an idea on how to achieve that?
    >
    >Regards,
    >Arnaud.



    Here is a really bad hack but it is the only way I could find out the
    number of columns.

    function getNumberOfColumns( oForm) {
    oForm.elements["one"].style.width = "100%";
    fullWidth = oForm.elements["one"].clientWidth;
    oForm.elements["one"].style.width = null;

    while( oForm.elements[ "one"].clientWidth < fullWidth) {
    oForm.elements["one"].cols = oForm.elements[ "one"].cols + 1;
    }
    oForm.elements["one"].cols = oForm.elements[ "one"].cols - 1;
    alert( oForm.elements["one"].cols);
    }

    I have only tested this in Internet Explorer 6.0. It will probably
    require some tweaking for other browsers.

    Peace, Vm
    Yaz
    , Jul 22, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. phl
    Replies:
    2
    Views:
    1,924
  2. arnaud
    Replies:
    0
    Views:
    85
    arnaud
    Jul 20, 2004
  3. fred
    Replies:
    3
    Views:
    253
    Zifud
    Mar 17, 2005
  4. stalin
    Replies:
    3
    Views:
    215
    Dr J R Stockton
    Jan 30, 2008
  5. Replies:
    5
    Views:
    857
Loading...

Share This Page