how to test the 'value' for unclicked radio buttons

B

bbxrider

i have 2 radio buttons 1 for yes and 1 for no, it is mandatory for one to be
checked, and am purposely leaving both blank on the page

when i tested for .value, when neither was checked, an alert message comes
back with literally 'undetermined',
and i get the message 2x, once for each button
alert("empty prevVolunteer is " + e.value);
i.e, the text in the message box is "empty prevVolunteer undetermined"

i haven't found anything similar to null, true, false, etc for
'undetermined, so i tried slicing it,
alert("empty prevVolunteer is " + e.value.slice(0,2));
and got 'ye' for the yes button and 'no' for the no button

so i'm confused, if the alert returned value is 'undetermined' and yet it
slices to the checked value, when neither has been checked, how do you check
if neither one has been checked
 
L

Lasse Reichstein Nielsen

bbxrider said:
i have 2 radio buttons 1 for yes and 1 for no, it is mandatory for one to be
checked, and am purposely leaving both blank on the page

I.e., you have code similar to:

<form name="bar">
Yes: <input type="radio" name="foo" value="yes">
No: <input type="radio" name="foo" value="no">
when i tested for .value, when neither was checked, an alert message comes
back with literally 'undetermined',

I bet it says "undefined".
and i get the message 2x, once for each button

Only if you ask for it twice.
alert("empty prevVolunteer is " + e.value);

var e = document.forms['bar'].elements['foo'];
alert("value is " + e.value);

alerts the text: "value is undefined"
i haven't found anything similar to null, true, false, etc for
'undetermined,

"undefined" is a the value Javascript gives to uninitialized variables
and nonexisting object properties. If "e.value" is "undefined", then
it is because e has no value property.

This is correct behavior. The value of "e" in the above is an "Element
Array" (i.e., perhaps not really an array, but it looks sufficiently
like it). It has two elements, one for each radiobutton in the group.

In order to see whether no radiobutton has been selected, you must
check them both. There is no shorthand for "the radiobutton that is
checked".

if (!e[0].checked && !e[1].checked) { // neither selected ...
so i tried slicing it,
alert("empty prevVolunteer is " + e.value.slice(0,2));
and got 'ye' for the yes button and 'no' for the no button

The values of the radiobuttons is the one you have assigned them, no
surprice there. But then, you are looking at each button, which is not
the same as looking at the array containing them/
so i'm confused, if the alert returned value is 'undetermined' and yet it
slices to the checked value, when neither has been checked, how do you check
if neither one has been checked

See above ... by checking that neither is checked.
/L
 
B

bbxrider

thanks for the long reply, maybe i was hallucinating and got the undefined
instead of undetermined but after many hours of coding, i can develop
dyslexia
so now after reading more the button have a convenient 'checked' property,
that can be used testing for true and false. the coding will be ugly though.
the undefined should come in handy, and i understand there is also an
undeclared to fine tune all these clarifications

Lasse Reichstein Nielsen said:
bbxrider said:
i have 2 radio buttons 1 for yes and 1 for no, it is mandatory for one to be
checked, and am purposely leaving both blank on the page

I.e., you have code similar to:

<form name="bar">
Yes: <input type="radio" name="foo" value="yes">
No: <input type="radio" name="foo" value="no">
when i tested for .value, when neither was checked, an alert message comes
back with literally 'undetermined',

I bet it says "undefined".
and i get the message 2x, once for each button

Only if you ask for it twice.
alert("empty prevVolunteer is " + e.value);

var e = document.forms['bar'].elements['foo'];
alert("value is " + e.value);

alerts the text: "value is undefined"
i haven't found anything similar to null, true, false, etc for
'undetermined,

"undefined" is a the value Javascript gives to uninitialized variables
and nonexisting object properties. If "e.value" is "undefined", then
it is because e has no value property.

This is correct behavior. The value of "e" in the above is an "Element
Array" (i.e., perhaps not really an array, but it looks sufficiently
like it). It has two elements, one for each radiobutton in the group.

In order to see whether no radiobutton has been selected, you must
check them both. There is no shorthand for "the radiobutton that is
checked".

if (!e[0].checked && !e[1].checked) { // neither selected ...
so i tried slicing it,
alert("empty prevVolunteer is " + e.value.slice(0,2));
and got 'ye' for the yes button and 'no' for the no button

The values of the radiobuttons is the one you have assigned them, no
surprice there. But then, you are looking at each button, which is not
the same as looking at the array containing them/
so i'm confused, if the alert returned value is 'undetermined' and yet it
slices to the checked value, when neither has been checked, how do you check
if neither one has been checked

See above ... by checking that neither is checked.
/L
 

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

Forum statistics

Threads
474,079
Messages
2,570,573
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top