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

Syntax help please

P: n/a
I'm trying to put:

document.form1.proto0[0].checked=true;
document.form1.proto1[0].checked = true;
document.form1.proto2[0].checked = true;
..
..
document.form1.proton[0].checked =true;

into a loop. I've unsuccessfully tried:

for (var i=0;i<n+1;i++) {
document.form1.proto+i+[0].checked=true;
}

and I've been unsuccessful using:

for (var i=0;i<n+1;i++) {
document.forms["form1"].elements["proto"+i+"[0]"].checked=true;
}

Syntax-wise, where am I going astray?

--
Ed Jay (remove 'M' to reply by email)

Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
Sep 6 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Ed Jay <ed***@aes-intl.comwrites:
I'm trying to put:

document.form1.proto0[0].checked=true;
document.form1.proto1[0].checked = true;
document.form1.proto2[0].checked = true;
.
.
document.form1.proton[0].checked =true;

into a loop.
Smart move. :)

....
and I've been unsuccessful using:

for (var i=0;i<n+1;i++) {
document.forms["form1"].elements["proto"+i+"[0]"].checked=true;
This was close!
}

Syntax-wise, where am I going astray?
The [0] is not part of the name. The name is "proto2", and there
are apparently more elements in the form with that name.
Try:

var elems = document.form["form1"].elements;
for(var i = 0; i < n; i++) {
elems["proto"+i][0].checked = true;
}

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 6 '08 #2

P: n/a
Ed Jay wrote on 06 sep 2008 in comp.lang.javascript:
document.forms["form1"].elements["proto"+i+"[0]"].checked=true;
Methinks:

document.forms["form1"].elements["proto"+i][0].checked=true;

[If these are radio buttons]
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 6 '08 #3

P: n/a
Evertjan. wrote:
>Ed Jay wrote on 06 sep 2008 in comp.lang.javascript:
>document.forms["form1"].elements["proto"+i+"[0]"].checked=true;

Methinks:

document.forms["form1"].elements["proto"+i][0].checked=true;

[If these are radio buttons]
They are, indeed. I have several rows of two buttons each that I'm setting
if a preceding condition is met.

--
Ed Jay (remove 'M' to reply by email)

Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
Sep 6 '08 #4

P: n/a
Lasse Reichstein Nielsen wrote:
>Ed Jay <ed***@aes-intl.comwrites:
>I'm trying to put:

document.form1.proto0[0].checked=true;
document.form1.proto1[0].checked = true;
document.form1.proto2[0].checked = true;

into a loop.
>and I've been unsuccessful using:

for (var i=0;i<n+1;i++) {
document.forms["form1"].elements["proto"+i+"[0]"].checked=true;
}
>Syntax-wise, where am I going astray?

The [0] is not part of the name. The name is "proto2", and there
are apparently more elements in the form with that name.
Try:

var elems = document.form["form1"].elements;
for(var i = 0; i < n; i++) {
elems["proto"+i][0].checked = true;
}
Thanks, but it's not working. I continue to receive an error msg:
"Cannot convert undefined or null to Object." (The original multi-line,
non-loop command works fine.)

By way of elaboration, this issue pertains to multiple rows of radio button
pairs that I'm setting pursuant to a previous condition being met, e.g.,.
proto0[0] and proto0[1], etc.

--
Ed Jay (remove 'M' to reply by email)

Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
Sep 6 '08 #5

P: n/a
Ed Jay meinte:
Lasse Reichstein Nielsen wrote:
>var elems = document.form["form1"].elements;
^^^^
Typo - should be document.forms[...]

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
Sep 6 '08 #6

P: n/a
Gregor Kofler wrote:
>Ed Jay meinte:
>Lasse Reichstein Nielsen wrote:
>>var elems = document.form["form1"].elements;
^^^^
Typo - should be document.forms[...]
Teach me to cut and paste! :-)

Works.

--
Ed Jay (remove 'M' to reply by email)

Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
Sep 6 '08 #7

P: n/a
Ed Jay wrote:
>Lasse Reichstein Nielsen wrote:
>>Ed Jay <ed***@aes-intl.comwrites:
>>I'm trying to put:

document.form1.proto0[0].checked=true;
document.form1.proto1[0].checked = true;
document.form1.proto2[0].checked = true;

into a loop.
>>and I've been unsuccessful using:

for (var i=0;i<n+1;i++) {
document.forms["form1"].elements["proto"+i+"[0]"].checked=true;
}
>>Syntax-wise, where am I going astray?

The [0] is not part of the name. The name is "proto2", and there
are apparently more elements in the form with that name.
Try:

var elems = document.form["form1"].elements;
for(var i = 0; i < n; i++) {
elems["proto"+i][0].checked = true;
}
Thanks, but it's not working. I continue to receive an error msg:
"Cannot convert undefined or null to Object." (The original multi-line,
non-loop command works fine.)

By way of elaboration, this issue pertains to multiple rows of radio button
pairs that I'm setting pursuant to a previous condition being met, e.g.,.
proto0[0] and proto0[1], etc.
Now that Gregor Kofler has pointed out the typo, it works great. Thanks,
Lasse.

--
Ed Jay (remove 'M' to reply by email)

Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
Sep 6 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.