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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    TextBox TextArea Limit

    The following code is working fine

    ------------------------------------------------------

    <script type="text/javascript">
    function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit)
    {field.value = field.value.substring(0, maxlimit);}
    else
    {countfield.value = maxlimit - field.value.length;}
    }
    </script>

    <textarea id="A" onKeyDown="textCounter(this.form.A,this.form.A_read,100);" onKeyUp="textCounter(this.form.A,this.form.A_read,100);" ></textarea>

    <input type="Text" readonly id="A_read" value="100"/>

    ---------------------------------------------------------

    but how can I do it in an easier way ? in that way >>>

    --------------------------------------------------------


    <script type="text/javascript">
    function textCounter(field, maxlimit) {
    if (field.value.length > maxlimit)
    {field.value = field.value.substring(0, maxlimit);}
    else
    {field+"_read".value = maxlimit - field.value.length;}
    }
    </script>

    <textarea id="A" onKeyDown="textCounter(this,100);" onKeyUp="textCounter(this,100);"></textarea>
    <input type="Text" readonly id="A_read" value="100" />

    -----------------------------------------------

    thank you

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    field.form.elements[field.name+"_read"].value = maxlimit - field.value.length;

  • #3
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    it works

    thanks glenngv !
    it works with

    [field.name+"_read"].value = maxlimit - field.value.length;

    and not with

    field.form.elements[field.name+"_read"].value = maxlimit - field.value.length


    but the textBox A_read now doesn't show the numbers it keeps 100
    Last edited by angiras; 10-27-2003 at 08:52 AM.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    huh? it works with:

    [field.name+"_read"].value = maxlimit - field.value.length;



    maybe this doesn't work:

    field.form.elements[field.name+"_read"].value = maxlimit - field.value.length

    because you are using id instead of name in the field.

    ok, try this.

    document.getElementById(field.name+"_read").value = maxlimit - field.value.length;

  • #5
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    here it is

    all the code >>>>


    <?xml version="1.0" encoding="iso-8859-1"?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <title>Text Box limit</title>
    <script type="text/javascript">
    function textCounter(field, maxlimit)
    {
    if (field.value.length > maxlimit)
    {
    field.value = field.value.substring(0, maxlimit);
    }
    else
    {
    document.getElementById(field.name+"_read").value = maxlimit - field.value.length;
    }
    }
    </script>
    </head>
    <body>
    <form id="MainForm">

    <textarea id="A" onKeyDown="textCounter(this,100);" onKeyUp="textCounter(this,100);"></textarea>

    <input type="Text" readonly id="A_read" value="100"/>

    </form>
    </body>
    </html>


    --------------------------------

    and I get the message

    document.getElementById(....) is not an object



    :-((

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    ok, use field.id instead of field.name

    document.getElementById(field.id+"_read").value = maxlimit - field.value.length;

    also try the original code:

    field.form.elements[field.id+"_read"].value = maxlimit - field.value.length

    it might work now.

  • #7
    Regular Coder
    Join Date
    Dec 2002
    Location
    France
    Posts
    522
    Thanks
    0
    Thanked 0 Times in 0 Posts

    YES

    it works !!

    both are working

    field.form.elements[field.id+"_read"].value = maxlimit - field.value.length

    OR

    document.getElementById(field.id+"_read").value = maxlimit - field.value.length;


    thank you


  •  

    Posting Permissions

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