Multiple text boxes with the same name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ellie100

    #1

    Multiple text boxes with the same name

    Hi all

    I'm a first-time poster and would appreciate some help with a problem
    I'm having. I have an HTML form (UPD_BUDG) which includes 10
    checkboxes, all with the same name (Apply_Phasing_ CB) but different
    values. On its own (i.e. before I add any JS) the form works fine.

    I need to add an onSubmit function which counts how many of the boxes
    are checked. Here is the script I have used:

    function check_form()
    {
    var check_count = 0;
    for(i = 0; i < 10; i++)
    {
    if (document.forms .UPD_BUDG.Apply _Phasing_CB[i].checked)
    {
    check_count++;
    }
    }
    return check_count;
    }


    When I run this I get the error message
    'document.forms .UPD_BUDG.Apply _Phasing_CB[...].checked is not an
    object'. I am running this on IE 5.

    Can anyone advise me on what I'm doing wrong?

    Thanks

    Ellie
  • Janwillem Borleffs

    #2
    Re: Multiple text boxes with the same name


    "Ellie100" <elliespostbox@ hotmail.com> schreef in bericht
    news:45ce4730.0 307230726.6709d 57e@posting.goo gle.com...[color=blue]
    > I need to add an onSubmit function which counts how many of the boxes
    > are checked. Here is the script I have used:
    >[/color]
    ....[color=blue]
    >
    > When I run this I get the error message
    > 'document.forms .UPD_BUDG.Apply _Phasing_CB[...].checked is not an
    > object'. I am running this on IE 5.
    >
    > Can anyone advise me on what I'm doing wrong?
    >[/color]

    Must be an error in your HTML (didn't close the form tag perhaps?), because
    the following works for me:

    <html>
    <head>
    <title> New Document </title>
    <script type="text/javascript">
    function check_form() {
    var obj = document.UPD_BU DG.Apply_Phasin g_CB;
    var check_count = 0;
    for (var i = 0; i < obj.length; i++) {
    if (obj[i].checked) check_count++;
    }
    return check_count;
    }
    </script>
    </head>

    <body>
    <form name="UPD_BUDG" >
    <input type="checkbox" name="Apply_Pha sing_CB"><br />
    <input type="checkbox" name="Apply_Pha sing_CB"><br />
    <input type="checkbox" name="Apply_Pha sing_CB"><br />
    <input type="checkbox" name="Apply_Pha sing_CB"><br />
    <input type="checkbox" name="Apply_Pha sing_CB"><br />
    <input type="checkbox" name="Apply_Pha sing_CB"><br />
    <input type="checkbox" name="Apply_Pha sing_CB"><br />
    <input type="checkbox" name="Apply_Pha sing_CB"><br />
    <input type="checkbox" name="Apply_Pha sing_CB"><br />
    <input type="checkbox" name="Apply_Pha sing_CB"><br />
    <input type="button" onclick="alert( check_form())" value="Check">
    </form>
    </body>
    </html>


    JW



    Comment

    • Ellie100

      #3
      Re: Multiple text boxes with the same name

      "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> wrote in message news:<3f1efcab$ 0$28889$1b62eed f@news.euronet. nl>...[color=blue]
      >
      > Must be an error in your HTML (didn't close the form tag perhaps?), because
      > the following works for me:
      >
      > <html>
      > <head>
      > <title> New Document </title>
      > <script type="text/javascript">
      > function check_form() {
      > var obj = document.UPD_BU DG.Apply_Phasin g_CB;
      > var check_count = 0;
      > for (var i = 0; i < obj.length; i++) {
      > if (obj[i].checked) check_count++;
      > }
      > return check_count;
      > }
      > </script>
      > </head>
      >
      > <body>
      > <form name="UPD_BUDG" >
      > <input type="checkbox" name="Apply_Pha sing_CB"><br />
      > <input type="checkbox" name="Apply_Pha sing_CB"><br />
      > <input type="checkbox" name="Apply_Pha sing_CB"><br />
      > <input type="checkbox" name="Apply_Pha sing_CB"><br />
      > <input type="checkbox" name="Apply_Pha sing_CB"><br />
      > <input type="checkbox" name="Apply_Pha sing_CB"><br />
      > <input type="checkbox" name="Apply_Pha sing_CB"><br />
      > <input type="checkbox" name="Apply_Pha sing_CB"><br />
      > <input type="checkbox" name="Apply_Pha sing_CB"><br />
      > <input type="checkbox" name="Apply_Pha sing_CB"><br />
      > <input type="button" onclick="alert( check_form())" value="Check">
      > </form>
      > </body>
      > </html>
      >
      >
      > JW[/color]

      Thanks JW. Referring to the object as a variable, as you did, rather
      than directly, seems to have done the trick. If anyone can explain
      why this should be, I'd be interested!

      Thanks again

      Ellie

      Comment

      Working...