(Not so) Simple javascript question

W

WJ

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!
 
C

C A Upsdell

WJ said:
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.
 
R

rf

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.
 
M

Michael Winter

[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
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
 
M

Michael Winter

On Tue, 12 Oct 2004 21:37:26 -0400, C A Upsdell

[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top