By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,742 Members | 2,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,742 IT Pros & Developers. It's quick & easy.

Basic array stuff

P: n/a
This is kind of basic, and I googled but didn't find much help.

I have an array of text element fields. They all need to have the same
name. So, let the name be either all with a[] or build individually as
a[0], a[1], a[2], etc. The brackets need to be there because I will
obtaining the values of them as an array using $_POST{'a'] in php.

What I want to do is in javascript set the value of an individual
element, say a[3]. I have tried many combinations of

1 - setting the ids all as
a) a
b) a[]
c) individually as a[0], a[1], etc.
2 - using document.getElementById
3 - using document.getElementsByName

Any time I create and object from the name, and then try to use that
object, I get "object has no properties" in Firebug. This is true
whether I try obj.value or obj[i].value.

So, please,
1 - How do I set the id attribute?
2 - How do I access or set an individual member in the array?
3 - For the name attribute, should I use a[] or individually build them
or does it not matter as far as javascript is concerned?

Yes, this is basic, but I have been going in circles for a few hours now
so any help will be appreciated.
Oct 5 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On Sun, 05 Oct 2008 15:38:49 -0400, sheldonlg <sheldonlgwrote:
>This is kind of basic, and I googled but didn't find much help.

I have an array of text element fields. They all need to have the same
name. So, let the name be either all with a[] or build individually as
a[0], a[1], a[2], etc. The brackets need to be there because I will
obtaining the values of them as an array using $_POST{'a'] in php.

What I want to do is in javascript set the value of an individual
element, say a[3]. I have tried many combinations of

1 - setting the ids all as
a) a
b) a[]
c) individually as a[0], a[1], etc.
2 - using document.getElementById
3 - using document.getElementsByName

Any time I create and object from the name, and then try to use that
object, I get "object has no properties" in Firebug. This is true
whether I try obj.value or obj[i].value.

So, please,
1 - How do I set the id attribute?
2 - How do I access or set an individual member in the array?
3 - For the name attribute, should I use a[] or individually build them
or does it not matter as far as javascript is concerned?

Yes, this is basic, but I have been going in circles for a few hours now
so any help will be appreciated.
Pleast post question to comp.lang.javascript.
This group is about basic html.
Oct 5 '08 #2

P: n/a
On Oct 5, 9:17*pm, richard <mem...@newsguy.comwrote:
On Sun, 05 Oct 2008 15:38:49 -0400, sheldonlg <sheldonlgwrote:
>>
Pleast post question to comp.lang.javascript.
This group is about basic html.- Hide quoted text -

For one of the two values of this, it is. Check where you are
writing.

--
(c) John Stockton, near London, UK. Posting with Google.
Mail: J.R.""""""""@physics.org or (better) via Home Page at
Web: <URL:http://www.merlyn.demon.co.uk/>
FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ....|
Oct 5 '08 #3

P: n/a
On 10/05/08 12:38 pm, sheldonlg wrote:
>
I have an array of text element fields. They all need to have the same
name. So, let the name be either all with a[] or build individually as
a[0], a[1], a[2], etc. The brackets need to be there because I will
obtaining the values of them as an array using $_POST{'a'] in php.
How is that "array of text fields" created in HTML?
What I want to do is in javascript set the value of an individual
element, say a[3]. I have tried many combinations of
PHP is a server-side program. Javascript is a client-side language. What
exists in PHP on the server is not accessible by the client.
Using document.getElementById() is ineffective since the ID attribute
must be unique within a document.
Using document.getElementsByName() might work by calling it repeatedly
until a null is returned. But not all browsers work that way. (ISTR some
browsers return only the first instance, others only the last, yet others
all of them.)
BTW: What happens for a client that has JS disabled?
>
Any time I create an object from the name, and then try to use that
object, I get "object has no properties" in Firebug. This is true
whether I try obj.value or obj[i].value.
An URL to a test case would be most useful.

--
jmm (hyphen) list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Oct 5 '08 #4

P: n/a
I finally got it to where it works (www.sheldonlg.com/t.php)

The simple answer was to have the names and ids all as "a[]". The
javascript does:

obj = document.getElementsByName('t[]');

and the values are at obj[i].value.
Oct 6 '08 #5

P: n/a
On 10/05/08 06:10 pm, sheldonlg wrote:
I finally got it to where it works (www.sheldonlg.com/t.php)
Oddly, the "js" button does nothing. Is it supposed to?
The simple answer was to have the names and ids all as "a[]". The
javascript does:
And is invalid markup. IDs are required to be unique within a document.

--
jmm (hyphen) list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Oct 6 '08 #6

P: n/a
Jim Moe <jm***************@sohnen-moe.comwrote:
sheldonlg wrote:
>I finally got it to where it works *(www.sheldonlg.com/t.php)

* Oddly, the "js" button does nothing. Is it supposed to?
It alerts 11 and 22 in a row here. Maybe it does work work with you
because the markup is not Valid, duplicate IDs aside:

<http://validator.w3.org/check?verbos...uri=http%3A%2F
%2Fwww.sheldonlg.com%2Ft.php>
The simple answer was to have the names and ids all as "a[]". *The
javascript does:

* And is invalid markup. IDs are required to be unique within a document.
But not names.

BTW, you should fix your borken sender address so as to comply with
Internet standards and Netiquette.
F'up2 cljs

PointedEars
Oct 6 '08 #7

P: n/a
Thomas 'PointedEars' Lahn wrote:
Jim Moe <jm***************@sohnen-moe.comwrote:
>sheldonlg wrote:
>>I finally got it to where it works (www.sheldonlg.com/t.php)
Oddly, the "js" button does nothing. Is it supposed to?

It alerts 11 and 22 in a row here. Maybe it does work work with you
because the markup is not Valid, duplicate IDs aside:

<http://validator.w3.org/check?verbos...uri=http%3A%2F
%2Fwww.sheldonlg.com%2Ft.php>
>>The simple answer was to have the names and ids all as "a[]". The
javascript does:
And is invalid markup. IDs are required to be unique within a document.

But not names.

BTW, you should fix your borken sender address so as to comply with
Internet standards and Netiquette.
F'up2 cljs

PointedEars
I fixed a couple of errors.

1 - Yes, the ids of t[] can all be removed as there is no need for these
ids and ids should not be duplicates. I used getElementsByName.

2 - The complaint about the semi-colon in
for (i=0; i<obj.length; i++) {
is weird. That occurs between a <script></scriptinside the javascript
function. That is a proper specification for a for-loop. Why did it
complain? Here is the function:

<script type="text/javascript">
function dojs() {
var obj = document.getElementsByName('t[]');
for (i=0; i<obj.length; i++) {
alert('t[' + i + '] = ' + obj[i].value);
}
}
</script>
Oct 6 '08 #8

P: n/a
sheldonlg <sheldonlgwrote:
Thomas 'PointedEars' Lahn wrote:
<http://validator.w3.org/check?verbos...uri=http%3A%2F
%2Fwww.sheldonlg.com%2Ft.php>

[...]
2 - The complaint about the semi-colon in
* * * * for (i=0; i<obj.length; i++) {
You did to declare `i', which is error-prone, and you have a property
resolved repeatedly, which is inefficient:

for (var i = 0, len = obj.lengh; i < len; i++)
{
is weird. *That occurs between a <script></scriptinside the javascript
function. *That is a proper specification for a for-loop. *Why did it
complain?
Because in XHTML the content model of the `script' element is PCDATA,
not CDATA as in HTML. Therefore, `<' is regarded an STAGO (Start Tag
Open) delimiter and `;' is not part of a proper attribute name that is
expected in a start tag.

While the solutions for this "problem" are simple and have been
discussed also here several times -- Google is your friend [psf 6.1]
--, you should simply not declare XHTML when you don't know what you
are doing.

And apparently your sender header is borken as well. Fix it.
PointedEars
Oct 6 '08 #9

P: n/a
On 10/06/08 02:32 am, Thomas 'PointedEars' Lahn wrote:
>
BTW, you should fix your borken sender address so as to comply with
Internet standards and Netiquette.
Please be more specific.

--
jmm (hyphen) list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Oct 6 '08 #10

P: n/a
Thomas 'PointedEars' Lahn wrote:
sheldonlg <sheldonlgwrote:
>Thomas 'PointedEars' Lahn wrote:
>><http://validator.w3.org/check?verbos...uri=http%3A%2F
%2Fwww.sheldonlg.com%2Ft.php>
[...]
2 - The complaint about the semi-colon in
for (i=0; i<obj.length; i++) {

You did to declare `i', which is error-prone, and you have a property
resolved repeatedly, which is inefficient:
OK, I can but the resolving being inefficient and should have simply set
it once. However, I just don't understand what you are saying about

`i'

Where did I use that accent mark before the "i"?
>
for (var i = 0, len = obj.lengh; i < len; i++)
{
I think you have a typo here.
>is weird. That occurs between a <script></scriptinside the javascript
function. That is a proper specification for a for-loop. Why did it
complain?

Because in XHTML the content model of the `script' element is PCDATA,
not CDATA as in HTML. Therefore, `<' is regarded an STAGO (Start Tag
Open) delimiter and `;' is not part of a proper attribute name that is
expected in a start tag.
I don't understand what you are saying. Do you mean that because it is
defined as xhtml, that even though it is inside a javascript it treats
the "<" as an opening of a tag rather than as a "less than"? Is so,
then why would your suggestion abovenot have a complaint?
>
While the solutions for this "problem" are simple and have been
discussed also here several times -- Google is your friend [psf 6.1]
--, you should simply not declare XHTML when you don't know what you
are doing.
Apparently my default template in Dreamweaver for a new PHP file
produces that. I didn't pay much attention to it when I composed the
example case.
Oct 9 '08 #11

P: n/a
sheldonlg meinte:
OK, I can but the resolving being inefficient and should have simply set
it once. However, I just don't understand what you are saying about

`i'

Where did I use that accent mark before the "i"?
You should add *var* to declare it as a local variable. Perhaps you
should try JSLint to "validate" your scripts. It's a bit harsh on code
not following its guidelines, but a great tool to find such issues.
> for (var i = 0, len = obj.lengh; i < len; i++)
I think you have a typo here.
Yes, it should read "length".
>Because in XHTML the content model of the `script' element is PCDATA,
not CDATA as in HTML. Therefore, `<' is regarded an STAGO (Start Tag
Open) delimiter and `;' is not part of a proper attribute name that is
expected in a start tag.

I don't understand what you are saying. Do you mean that because it is
defined as xhtml, that even though it is inside a javascript it treats
the "<" as an opening of a tag rather than as a "less than"?
Yes.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Oct 9 '08 #12

P: n/a
Thomas 'PointedEars' Lahn wrote:
sheldonlg <sheldonlgwrote:
> for (i=0; i<obj.length; i++) {
JFTR:
You did to declare `i', which is error-prone, and you have a property
s/to/not/
resolved repeatedly, which is inefficient:

for (var i = 0, len = obj.lengh; i < len; i++)
{
for (var i = 0, len = obj.length; i < len; i++)
{
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Oct 10 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.