473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

reffering to input names with brackets in them

Hello! I have a nice little problem :)

I am using PHP to create input fields on a page, and a trick in PHP to have the
result end up in an array is to name the field like "field[foo]".

However, sometimes I need to act on these fields with a javascript, but as far
as I know, you can't do this:

<input type='text' name='field[foo]' />

<script language='javascript'>
document.form.field[foo].value = "bar"
</script>

Since that looks for the foo-entry in the field-array, right?

So how do I refer to the "field[foo]" field in a script?

--
Sandman[.net]
Jul 20 '05 #1
6 4805
Hello,

Try this:

<body>
<form name='f'>
<input type='button' name='x[]' onclick='accessme(this.form)' value='Hey!'>
</form>
<script>
function accessme(frm)
{
alert(frm.elements['x[]'].value);
}
</script>
</body>

"Sandman" <mr@sandman.net> wrote in message
news:mr**********************@news.fu-berlin.de...
Hello! I have a nice little problem :)

I am using PHP to create input fields on a page, and a trick in PHP to have the result end up in an array is to name the field like "field[foo]".

However, sometimes I need to act on these fields with a javascript, but as far as I know, you can't do this:

<input type='text' name='field[foo]' />

<script language='javascript'>
document.form.field[foo].value = "bar"
</script>

Since that looks for the foo-entry in the field-array, right?

So how do I refer to the "field[foo]" field in a script?

--
Sandman[.net]

Jul 20 '05 #2
Sandman wrote:
Hello! I have a nice little problem :)

I am using PHP to create input fields on a page, and a trick in PHP to have the
result end up in an array is to name the field like "field[foo]".

However, sometimes I need to act on these fields with a javascript, but as far
as I know, you can't do this:

<input type='text' name='field[foo]' />

<script language='javascript'>
language attribute is deprecated, use type="text/javascript" instead.

Its irrelevant to your problem though.
document.form.field[foo].value = "bar"
</script>

Since that looks for the foo-entry in the field-array, right?

So how do I refer to the "field[foo]" field in a script?

http://www.jibbering.com/faq/#FAQ4_39

--
Randy
Chance Favors The Prepared Mind

Jul 20 '05 #3
In article <Ld********************@comcast.com>,
Randy Webb <hi************@aol.com> wrote:
document.form.field[foo].value = "bar"
</script>

Since that looks for the foo-entry in the field-array, right?

So how do I refer to the "field[foo]" field in a script?

http://www.jibbering.com/faq/#FAQ4_39


Uh, so that means:

document.form["field[]"].value ?

--
Sandman[.net]
Jul 20 '05 #4
Sandman wrote:
In article <Ld********************@comcast.com>,
Randy Webb <hi************@aol.com> wrote:

document.form.field[foo].value = "bar"
</script>

Since that looks for the foo-entry in the field-array, right?

So how do I refer to the "field[foo]" field in a script?

http://www.jibbering.com/faq/#FAQ4_39

Uh, so that means:

document.form["field[]"].value ?


Close:

document.forms['formName'].elements['field[]'].value

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #5
In article <I-********************@comcast.com>,
Randy Webb <hi************@aol.com> wrote:
Uh, so that means:

document.form["field[]"].value ?


Close:

document.forms['formName'].elements['field[]'].value


Aaah, that worked! You actually had to refer to it by the elements[] - I only
thought this was a way to escape names, so if you have an object named 'foo[]'
then you could refer to it by document['foo[]'] - but apprently not. :)

Thanks!

--
Sandman[.net]
Jul 20 '05 #6
Sandman wrote:
In article <I-********************@comcast.com>,
Randy Webb <hi************@aol.com> wrote:

Uh, so that means:

document.form["field[]"].value ?


Close:

document.forms['formName'].elements['field[]'].value

Aaah, that worked! You actually had to refer to it by the elements[] - I only
thought this was a way to escape names, so if you have an object named 'foo[]'
then you could refer to it by document['foo[]'] - but apprently not. :)


Glad you got it figured out.

window['foo[]'] will work in some instances, as will document['foo[]']
but using, or attempting to use, that notation, without understanding
its implications/uses will cause you more problems than it will ever
solve. :-)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: J.R | last post by:
Greetings, I'm adding dynamically created input type='file' controls via JavaScript. However when I try to access they do not seem to be returned in the form collection. Any ideas? Thanks,...
3
by: Perdit | last post by:
I am wondering why this does not work in javascript. The html color is set to automatic (none) element1, element2, etc are input fields (textboxes) ...
3
by: williamc | last post by:
Somebody asked me if it would be possible to add auto-advance to a web form where there are a lot of repetitive 5 character fields. I took a look around the web and found a script, which appears to...
2
by: Michael Bulatovich | last post by:
I have a simple db to keep track of work/time/projects etc. It has two fields (column) named "start time" and "end time" WITH THE SPACES. I'm trying to do some automation to a form associated with...
1
by: John | last post by:
Hi What would be the input mask for a name field where first letter is caps and the rest lower? Its ideal if the mask can handle multiple names separated by a space but if not a single word will...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
1
by: christianlott1 | last post by:
I want to provide users with an interface to create a custom merge (all in Access, not Word). User will put in a set of brackets ("<>") in a memo field and when they click the merge button it will...
12
by: Wayne | last post by:
I have been given the task of rewriting a database that seems as though it has been written by someone with a very basic understanding of Access. Many of the object names and field names in tables...
13
by: cront | last post by:
I have a problem to work on: we will ask user to input anything and we will put that back onto the standard output with all set of brackets removed. We will not remove any single bracket e.g. ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.