Connecting Tech Pros Worldwide Forums | Help | Site Map

Syntax help please

Ed Jay
Guest
 
Posts: n/a
#1: Sep 6 '08
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

Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#2: Sep 6 '08

re: Syntax help please


Ed Jay <edMbj@aes-intl.comwrites:
Quote:
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. :)

....
Quote:
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!
Quote:
}
>
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.'
Evertjan.
Guest
 
Posts: n/a
#3: Sep 6 '08

re: Syntax help please


Ed Jay wrote on 06 sep 2008 in comp.lang.javascript:
Quote:
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)
Ed Jay
Guest
 
Posts: n/a
#4: Sep 6 '08

re: Syntax help please


Evertjan. wrote:
Quote:
>Ed Jay wrote on 06 sep 2008 in comp.lang.javascript:
>
Quote:
>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
Ed Jay
Guest
 
Posts: n/a
#5: Sep 6 '08

re: Syntax help please


Lasse Reichstein Nielsen wrote:
Quote:
>Ed Jay <edMbj@aes-intl.comwrites:
>
Quote:
>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.
>
Quote:
>and I've been unsuccessful using:
>>
>for (var i=0;i<n+1;i++) {
>document.forms["form1"].elements["proto"+i+"[0]"].checked=true;
>>}
>
Quote:
>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
Gregor Kofler
Guest
 
Posts: n/a
#6: Sep 6 '08

re: Syntax help please


Ed Jay meinte:
Quote:
Lasse Reichstein Nielsen wrote:
Quote:
Quote:
>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
Ed Jay
Guest
 
Posts: n/a
#7: Sep 6 '08

re: Syntax help please


Gregor Kofler wrote:
Quote:
>Ed Jay meinte:
>
Quote:
>Lasse Reichstein Nielsen wrote:
>
Quote:
Quote:
>>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
Ed Jay
Guest
 
Posts: n/a
#8: Sep 6 '08

re: Syntax help please


Ed Jay wrote:
Quote:
>Lasse Reichstein Nielsen wrote:
>
Quote:
>>Ed Jay <edMbj@aes-intl.comwrites:
>>
Quote:
>>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.
>>
Quote:
>>and I've been unsuccessful using:
>>>
>>for (var i=0;i<n+1;i++) {
>>document.forms["form1"].elements["proto"+i+"[0]"].checked=true;
>>>}
>>
Quote:
>>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
Closed Thread