(Not so) Simple javascript question

Discussion in 'HTML' started by WJ, Oct 13, 2004.

  1. WJ

    WJ Guest

    I have a snippet of sample code. It walks the DOM to get the value of a
    select box.

    <html>
    <head>
    <script language="javascript">
    function doIt()
    { alert (document.testForm.Items.value);}
    </script>
    </head>
    <body>
    <form name="testForm">
    <table>

    <tr><td>
    <select name="Items">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    </select>
    </td></tr>
    <tr><td>
    <input type="button" name="submit" value="Submit!" onclick="javascript:
    doIt();">
    </td></tr>
    </form>
    </body>

    Runs fine. However, if I change the name of the select box from Items to
    Items[0], I get errors:

    <html>
    <head>
    <script language="javascript">
    function doIt()
    { alert (document.testForm.Items[0].value);}
    </script>
    </head>
    <body>
    <form name="testForm">
    <table>

    <tr><td>
    <select name="Items[0]">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    </select>
    </td></tr>
    <tr><td>
    <input type="button" name="submit" value="Submit!" onclick="javascript:
    doIt();">
    </td></tr>
    </form>
    </body>

    Now, I know the obvious thing is to not use [0]. The reason this is there
    is there are more than one of these, several in fact.
    This code is generated by jsp, so I really have to leave the array item in
    there, or else re-do the entire module. So if I can pull
    the values out like this, it would be the best.

    Thanks!
     
    WJ, Oct 13, 2004
    #1
    1. Advertising

  2. WJ

    C A Upsdell Guest

    "WJ" <> wrote in message
    news:Qj%ad.26169$hj.24660@fed1read07...
    >I have a snippet of sample code. It walks the DOM to get the value of a
    > select box.
    >
    > Now, I know the obvious thing is to not use [0]. The reason this is there
    > is there are more than one of these, several in fact.
    > This code is generated by jsp, so I really have to leave the array item in
    > there, or else re-do the entire module. So if I can pull
    > the values out like this, it would be the best.


    Item[0] is not a valid name.

    Can your jsp generate names like item_1, item_2, etc.? This should fix your
    problem.
     
    C A Upsdell, Oct 13, 2004
    #2
    1. Advertising

  3. WJ

    rf Guest

    WJ wrote:

    Nope. quite simple.

    > { alert (document.testForm.Items[0].value);}


    document.testForm.Items[0] is an the first object in the Items array. You do
    not have an Items array, you have an object called "Item[0]". That is what
    it is getting cranky about.

    Hmmm. If, as you say, you really need such a name then don't try to access
    it this way. Use

    document.getElementById("Item[0]").value

    Item[0] is now being treated as a string, not an array reference.

    Oh, and you should then use the id attribute instead of name:

    <select id="Items[0]">

    Not tested on all browsers, works in IE6.

    --
    Cheers
    Richard.
     
    rf, Oct 13, 2004
    #3
  4. On Wed, 13 Oct 2004 01:58:02 GMT, rf <rf@.invalid> wrote:

    [snip]

    > Use
    >
    > document.getElementById("Item[0]").value


    Or, more reasonably:

    formRef.elements['Item[0]']

    making the complete reference:

    document.forms['testForm'].elements['Item[0]'].value

    See sections 4.13, 4.25, and 4.39 (and the linked articles) at
    <URL:http://jibbering.com/faq/>.

    > Item[0] is now being treated as a string, not an array reference.
    >
    > Oh, and you should then use the id attribute instead of name:


    Either works with the code above, though using the name attribute will be
    supported by older browsers. However, it should be noted that square
    brackets are not legal characters in id attribute values (see second
    bullet in <URL:http://www.w3.org/TR/html4/types.html#h-6.2>).

    [snip]

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Oct 13, 2004
    #4
  5. On Tue, 12 Oct 2004 21:37:26 -0400, C A Upsdell
    <cupsdell0311XXX@-@> wrote:

    [snip]

    > Item[0] is not a valid name.


    Yes, it is. It just has to be accessed differently. See my other post in
    this thread.

    > Can your jsp generate names like item_1, item_2, etc.? This should fix
    > your problem.


    Though there's certainly no problem with doing that.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Oct 14, 2004
    #5
    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. Replies:
    0
    Views:
    586
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    694
    Kevin Spencer
    Jun 25, 2004
  3. Dave E
    Replies:
    7
    Views:
    772
    Josh Twist
    Jan 11, 2006
  4. Dave E
    Replies:
    7
    Views:
    224
    Josh Twist
    Jan 11, 2006
  5. Jacob N.
    Replies:
    0
    Views:
    101
    Jacob N.
    Dec 29, 2010
Loading...

Share This Page