change radio box checked status

L

laszlo

Please help me.

I have a radio box set, name is in rname variable, want to change the
checked status to the indexed box, index is in ii variable.

I tried

d = getElementById(rname);
d.value = ii; //this works fine if rname is text, textarea
if (d.type == radio) d[ii].checked = true;

I get for the above line an undefined is null or not an object error
for the d[ii].checked = true; statement.

If I set:
d.checked = true;

i.e. without index it sets the very first box to checked

Question: how to set checked the last (or arbitrary in sequence)
radiobox?
 
M

Michael Winter

laszlo wrote on 25 Nov 2003:

if (d.type == radio) d[ii].checked = true;

<snip>

The 'type' property returns a string. You need to change the if
expression so that the line reads:

if ('radio' == d.type) d[ii].checked = true;
^ ^

Mike
 
L

laszlo

Michael Winter said:
laszlo wrote on 25 Nov 2003:

if (d.type == radio) d[ii].checked = true;

<snip>

The 'type' property returns a string. You need to change the if
expression so that the line reads:

if ('radio' == d.type) d[ii].checked = true;
^ ^

Mike

You are right. It is typing error. Still for the line

if ('radio' == d.type) d[ii].checked = true;

I get an "'undefined' is null or not an object" error, apparently referring to

d[ii].checked = true

statment, even if I set ii=0;

laszlo
 
M

Michael Winter

laszlo wrote on 26 Nov 2003:

You are right. It is typing error. Still for the line

if ('radio' == d.type) d[ii].checked = true;

I get an "'undefined' is null or not an object" error,
apparently referring to

d[ii].checked = true

statment, even if I set ii=0;

You're using getElementById() to obtain an array of elements. That
method will only return one element, so subscripting is invalid. A
radio group uses the name attribute to identify the group. Use
getElementByName() instead. This will return a collection of all
elements using that name.

Mike
 
M

Michael Winter

Michael Winter wrote on 26 Nov 2003:

You're using getElementById() to obtain an array of elements. That
method will only return one element, so subscripting is invalid. A
radio group uses the name attribute to identify the group. Use
getElementByName() instead. This will return a collection of all
elements using that name.

Correction: The method is 'getElementsByName()'. I omitted the
plural.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top