Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   HTML (http://www.velocityreviews.com/forums/f31-html.html)
-   -   (Not so) Simple javascript question (http://www.velocityreviews.com/forums/t159643-not-so-simple-javascript-question.html)

WJ 10-13-2004 01:14 AM

(Not so) Simple javascript question
 
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 A Upsdell 10-13-2004 01:37 AM

Re: (Not so) Simple javascript question
 
"WJ" <wj@nospam.com> 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.



rf 10-13-2004 01:58 AM

Re: (Not so) Simple javascript question
 
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.



Michael Winter 10-13-2004 03:03 AM

Re: (Not so) Simple javascript question
 
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 10-14-2004 07:44 PM

Re: (Not so) Simple javascript question
 
On Tue, 12 Oct 2004 21:37:26 -0400, C A Upsdell
<cupsdell0311XXX@-@-@XXXrogers.com> 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.


All times are GMT. The time now is 06:06 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.