count chars?

Discussion in 'Javascript' started by roN, Feb 15, 2006.

  1. roN

    roN Guest

    Hey,

    I got following:
    HTML:
    <form name="Formular" method="post" onSubmit="return chkFormular()"
    action="mail.php" enctype="text/plain">
    ....
    ....
    ....
    <td align="right" valign="top" class="text"><font
    color='red'>*</font>Comments/Questions:<br>
    <input name="input" size="1"
    style="border-width:0;background-color:#FFFFFF"
    onfocus="if(this.blur)this.blur()">
    chars left</td>
    <td class="text"> <textarea name="comments"
    onkeyup="count(event)" rows="8" cols="40" class="flat"></textarea>
    </td>
    
    [js]
    <script language="JavaScript">
    var max = 255;

    document.Formular.input1.value = max;
    document.Formular.comments.focus();

    function count(e) {
    if (!e.which) keyCode = event.keyCode; // ie5+ op5+
    else keyCode = e.which; // nn6+

    if (document.Formular.comments.value.length<max+1)
    document.Formular.input1.value =
    max-document.Formular.comments.value.length;
    else {
    document.Formular.comments.value =
    document.Formular.comments.value.substring(0,max);
    document.Formular.input1.value = 0;
    }
    }
    </script>
    [/js]
    That's supposed to display how many chars are left besides the textarea,
    decreasing from 255. But it doesn't display anything, why not? Thank you
    guys!
    --
    chEErs roN

    I'm root, I'm allowed to do this! ;)
    keep on rockin'
    roN, Feb 15, 2006
    #1
    1. Advertising

  2. roN

    RobG Guest

    roN wrote:
    > Hey,
    >
    > I got following:
    >
    HTML:
    > <form name="Formular" method="post" onSubmit="return chkFormular()"
    > action="mail.php" enctype="text/plain">
    > ...
    > ...
    > ...
    > <td align="right" valign="top" class="text"><font
    > color='red'>*</font>Comments/Questions:<br>
    >                 <input name="input" size="1"
    > style="border-width:0;background-color:#FFFFFF"
    > onfocus="if(this.blur)this.blur()">
    >                 chars left</td>
    >               <td class="text"> <textarea name="comments"
    > onkeyup="count(event)" rows="8" cols="40" class="flat"></textarea>
    >               </td>
    > 
    > [js]
    > <script language="JavaScript">
    > var max = 255;
    >
    > document.Formular.input1.value = max;


    The script console told me that document.Formular.input1 has no
    properties. That is because your input has a name of 'input'. Change
    it to input1 and it 'works'.


    > document.Formular.comments.focus();
    >
    > function count(e) {
    > if (!e.which) keyCode = event.keyCode; // ie5+ op5+
    > else keyCode = e.which; // nn6+
    >
    > if (document.Formular.comments.value.length<max+1)
    > document.Formular.input1.value =
    > max-document.Formular.comments.value.length;
    > else {
    > document.Formular.comments.value =
    > document.Formular.comments.value.substring(0,max);
    > document.Formular.input1.value = 0;
    > }


    That appears to be an extremely lengthy way to go about things. Why not
    change the call to:

    <textarea name="comments" onkeyup="count(this)" ... >


    And the function to:

    function count(el)
    {
    document.Formular.input1.value = max - el.value.length;
    }

    [...]


    --
    Rob
    RobG, Feb 15, 2006
    #2
    1. Advertising

  3. RobG wrote:

    > That appears to be an extremely lengthy way to go about things.
    > Why not change the call to:
    >
    > <textarea name="comments" onkeyup="count(this)" ... >
    >
    >
    > And the function to:
    >
    > function count(el)
    > {
    > document.Formular.input1.value = max - el.value.length;


    document.forms['Formular'].elements['input1'].value =
    max - el.value.length;

    is (almost) standards compliant (apart from `document') and backwards
    compatible.


    PointedEars
    Thomas 'PointedEars' Lahn, Feb 16, 2006
    #3
    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. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,282
    Tim Rentsch
    Sep 23, 2005
  2. grocery_stocker

    Why does program only count 3 chars

    grocery_stocker, Nov 2, 2005, in forum: C++
    Replies:
    4
    Views:
    302
    John Harrison
    Nov 2, 2005
  3. Hongyu
    Replies:
    9
    Views:
    897
    James Kanze
    Aug 8, 2008
  4. David RF

    Count chars (not bytes) in UTF8 strings

    David RF, May 8, 2009, in forum: C Programming
    Replies:
    5
    Views:
    532
    Ben Bacarisse
    May 8, 2009
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    224
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page