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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Counting characters Text Area: replace two words, and it won't work

    Hello


    I have this snippet for controlling the number of characters entered in a textarea. It works.

    But, if I replace in this code the name "my_form" by "contact-form" and "my_text" by "message", it doesnt ! It looks like the keyword is "my_form". You change that , and the script will not work!

    It turns out that I have to change those names because the form as it was already had those names my_form and message before I added this counter And I dont want to replace the names of the form in order to adapt them to this code snippet below.

    The question is why would the script not work if I have just replaced two words, which dont seem to be keywords of JS programming (my_form and my_text)

    <html><head>
    <title>(Type a title for your page here)</title>

    <script language=JavaScript>
    <!--
    function check_length(my_form)
    {
    maxLen = 50; // max number of characters allowed
    if (my_form.my_text.value.length >= maxLen) {
    // Alert message if maximum limit is reached.
    // If required Alert can be removed.
    var msg = "You have reached your maximum limit of characters allowed";
    alert(msg);
    // Reached the Maximum length so trim the textarea
    my_form.my_text.value = my_form.my_text.value.substring(0, maxLen);
    }
    else{ // Maximum length not reached so update the value of my_text counter
    my_form.text_num.value = maxLen - my_form.my_text.value.length;}
    }
    //-->
    </script>

    </head>

    <body>
    <form name=my_form method=post>
    <textarea onKeyPress=check_length(this.form); onKeyDown=check_length(this.form); name=my_text rows=4 cols=30></textarea>
    <br>
    <input size=1 value=50 name=text_num> Characters Left
    </form>

    </body>
    </html>
    Last edited by alexei; 12-23-2010 at 03:30 PM.

  • #2
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I found the answer. For whatever the reason, it seems that my_form is a JS keyword. Couldnt find it in the doc, but did the trick

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    my_form is not a js keyword.

    The trouble is the illegal hyphen in contact-form. Change that to contact_form and it works fine.

    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.
    The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.




    "...there are indications that the severest phase of the recession is over..."
    - Harvard Economic Society (HES) Jan 18, 1930


  •  

    Tags for this Thread

    Posting Permissions

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