From: Kai Schaetzl on
I have a quick question if there is any way to access the following form
element in an HTML page by referencing the given name (which obviously is
not correct Javascript associative array syntax but necessary on the
server side):

<input type="radio" name="something[whatever]" ...>

(I use that to populate an array on the server side and Javascript wasn't
used until now where I need it for just a tiny but necessary innertext
modification based on what's checked.)

I found that I can use for instance

<input type="radio" id="whatever_1" name="something[whatever]" ...>

to access it by whatever_1, but I would rather use what's already there.

Any chance?

Kai

From: Richard Cornford on
On Mar 30, 2:22 pm, Kai Schaetzl wrote:
> I have a quick question if there is any way to access the
> following form element in an HTML page by referencing the
> given name (which obviously is not correct Javascript
> associative array syntax but necessary on the
> server side):
>
> <input type="radio" name="something[whatever]" ...>
>
> (I use that to populate an array on the server side and
> Javascript wasn't used until now where I need it for just a
> tiny but necessary innertext modification based on what's
> checked.)
>
> I found that I can use for instance
>
> <input type="radio" id="whatever_1" name="something[whatever]" ...>
>
> to access it by whatever_1, but I would rather use what's already there.
>
> Any chance?

<URL: http://jibbering.com/faq/#propertyAccess >

(The fragment identifier on that URL is not reliable cross-browser,
but if it doesn't work then read the table of contents.)

Richard.
From: The Natural Philosopher on
Kai Schaetzl wrote:
> I have a quick question if there is any way to access the following form
> element in an HTML page by referencing the given name (which obviously is
> not correct Javascript associative array syntax but necessary on the
> server side):
>
> <input type="radio" name="something[whatever]" ...>
>
> (I use that to populate an array on the server side and Javascript wasn't
> used until now where I need it for just a tiny but necessary innertext
> modification based on what's checked.)
>
> I found that I can use for instance
>
> <input type="radio" id="whatever_1" name="something[whatever]" ...>
>
> to access it by whatever_1, but I would rather use what's already there.
>
> Any chance?
>
yes and no.

IE6 at least makes no distinction between ID and name, so you need to be
careful. But I have plenty of success with using getElementByName with
indexed or non indexed names although ISTR some gotcha in how you
specify the array..maybe its 'all text'

Also IE6 is abysmal in sarch speed on any DOM objects.

I refuse to support anything earlier.


> Kai
>
From: Kai Schaetzl on
Richard Cornford schrieb am Tue, 30 Mar 2010 06:28:34 -0700 (PDT):

> <URL: http://jibbering.com/faq/#propertyAccess >

Thank you for the quick answer, the pointer to the FAQ and this specific
anchor. Thanks!

I'm almost satisfied by the solution I have now:

Several of these with changing values:
<input type="radio" name="["order[travel]" value="...">

and use it like that:

var travel = document.buy.elements["order[travel]"];
if (travel[1].checked == true)

which makes me depend on the order.
I read http://jibbering.com/faq/faq_notes/square_brackets.html
and it seems there is no better way, for instance to access by value,
right?
I have only a few values and they probably won't change very often. So,
the order is probably not a big problem, but ...

Kai
--
Conactive Internet Services, Berlin, Germany

From: Kai Schaetzl on
The Natural Philosopher schrieb am Tue, 30 Mar 2010 14:38:35 +0100:

> IE6 at least makes no distinction between ID and name, so you need to be
> careful.

I have changed it now to the syntax pointed to by Richard.
One of the pages referenced in the FAQ is http://msdn.microsoft.com/en-
us/library/dd347041(VS.85).aspx
and that says "The namedItem method was introduced in Microsoft Internet
Explorer 6 This method first searches for an object with a matching id
attribute. If a match is not found, then the method searches for an object
with a matching name attribute, but only on those elements that are allowed
a name attribute."
And that seems to apply to document.form.elements["order[travel]"], as
well. So, you can identify by id if an id is there, otherwise it uses the
name.

I tried the document.all.namedItem["order[travel]"] method, but it didn't
work for me, anyway.


Kai
--
Conactive Internet Services, Berlin, Germany