check if radio is checked

D

Diogenes

David said:
Best to use:
Or:
document.forms.myform.elements.owned_business[0].checked

And this syntax is very readable once you become used to it.

If one thoughtfully creates names (or id's) that reveal
the context of the object, you will be able to read your
code 2 years later and immediately understand what you were
doing back then.

This creative naming process can be APINT too; it's so easy to
defer this mode of thinking and just use X or Y instead.

But good programming is hard work, IMHO, or at least it should be.

Cheers
-Dio
 
R

Ron Eggler

Hi,

I would like to run a jvascript, checking if the radio button is checked.
My radio button looks like:

<input type="radio" name="owned_business" value="yes">

and i would like to check it with this:

alert (document.myform.owned_business.checked);
if (document.myform.owned_business.checked == false)

but alert returns me "undefined" and i don't understand why, my form name
is "myform" and the name of the radio is "owned_business" so i'm not seeing
where i go wrong.
Can anyone help me?

Thanks,
Ron
 
N

nolo contendere

Hi,

I would like to run a jvascript, checking if the radio button is checked.
My radio button looks like:

<input type="radio" name="owned_business" value="yes">

and i would like to check it with this:

        alert (document.myform.owned_business.checked);
        if (document.myform.owned_business.checked == false)

but alert returns me "undefined" and i don't understand why, my form name
is "myform" and the name of the radio is "owned_business" so i'm not seeing
where i go wrong.
Can anyone help me?

There's something weird with Radio buttons. I believe you need to
iterate through the set, instead of grabbing the checked one. Try
something like this:

function get_checked_radio( radio, form ) {
for ( var i = 0; i < form.length; i++ ) {
var e = form.elements;
var ename = e.name;
if ( ename != radio ) continue;
if ( e.checked ) return e.value;
}
return "";
}
 
N

nolo contendere

Hi,

I would like to run a jvascript, checking if the radio button is checked.
My radio button looks like:

<input type="radio" name="owned_business" value="yes">

and i would like to check it with this:

        alert (document.myform.owned_business.checked);
        if (document.myform.owned_business.checked == false)

but alert returns me "undefined" and i don't understand why, my form name
is "myform" and the name of the radio is "owned_business" so i'm not seeing
where i go wrong.
Can anyone help me?

Try something like this:

function getSelectedRadio(buttonGroup) {
// returns the array number of the selected radio button or -1 if
no button is selected
if (buttonGroup[0]) { // if the button group is an array (one
button is not an array)
for (var i=0; i<buttonGroup.length; i++) {
if (buttonGroup.checked) {
return i
}
}
} else {
if (buttonGroup.checked) { return 0; } // if the one button is
checked, return zero
}
// if we get to this point, no radio button is selected
return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
// returns the value of the selected radio button or "" if no
button is selected
var i = getSelectedRadio(buttonGroup);
if (i == -1) {
return "";
} else {
if (buttonGroup) { // Make sure the button group is an array
(not just one button)
return buttonGroup.value;
} else { // The button group is just the one button, and it is
checked
return buttonGroup.value;
}
}
} // Ends the "getSelectedRadioValue" function


not the prettiest or most efficient, I'm sure, but should get the job
done.
 
R

Ron Eggler

nolo said:
Hi,

I would like to run a jvascript, checking if the radio button is checked.
My radio button looks like:

<input type="radio" name="owned_business" value="yes">

and i would like to check it with this:

alert (document.myform.owned_business.checked);
if (document.myform.owned_business.checked == false)

but alert returns me "undefined" and i don't understand why, my form name
is "myform" and the name of the radio is "owned_business" so i'm not
seeing where i go wrong.
Can anyone help me?

There's something weird with Radio buttons. I believe you need to
iterate through the set, instead of grabbing the checked one. Try
something like this:

function get_checked_radio( radio, form ) {
for ( var i = 0; i < form.length; i++ ) {
var e = form.elements;
var ename = e.name;
if ( ename != radio ) continue;
if ( e.checked ) return e.value;
}
return "";
}


But why can't i just simply check the elemet's property?
Seems simpler to me.

Ron
 
R

Richard Cornford

Ron said:
Hi,

I would like to run a jvascript, checking if the radio button
is checked. My radio button looks like:

<input type="radio" name="owned_business" value="yes">

That is way to little context to get any sensible answer to the
question. Radio buttons are expected to be in sets (all with the same
name) and operate in a way that guarantees that at least one of them is
always checked. An single input element that is expected to have one of
two boolean states is best represented with a 'checkbox' element.
and i would like to check it with this:

alert (document.myform.owned_business.checked);
if (document.myform.owned_business.checked == false)

but alert returns me "undefined" and i don't understand why,

If you have more than one like-named radio button element in a form then
referencing them by name will give you an object that is a collection of
all of the like-named radio buttons. But then there is no sensible way
in which you can talk of "checking if _the_ radio button is checked".
my form name is "myform" and the name of the radio is
"owned_business" so i'm not seeing where i go wrong.
Can anyone help me?

Most of the couple of hundred ways in which you could "go wrong" here
would not result in "undefined" being alerted, but experience tells me
that an OP reporting something is not a valid indicator of it actually
being the case. (Demonstrations are the only convincing evidence that
something is the case, and demonstrations also provide full context for
whatever it is that they demonstrate).

See:-

<URL: http://jibbering.com/faq/faq_notes/form_access.html >

Richard.
 
A

Anthony Levensalor

Ron Eggler said:
Hi,

I would like to run a jvascript, checking if the radio button is checked.
My radio button looks like:

<input type="radio" name="owned_business" value="yes">

and i would like to check it with this:

alert (document.myform.owned_business.checked);
if (document.myform.owned_business.checked == false)

but alert returns me "undefined" and i don't understand why, my form name
is "myform" and the name of the radio is "owned_business" so i'm not seeing
where i go wrong.
Can anyone help me?

Thanks,
Ron


Try
alert(document.forms['myform']['owned_business'][0].checked);

Radios in my experience are accessed just like arrays, because the
browsers expect more than one. Incidentally, why not a checkbox?

~A!
 
N

nolo contendere

There's something weird with Radio buttons. I believe you need to
iterate through the set, instead of grabbing the checked one. Try
something like this:
function get_checked_radio( radio, form ) {
  for ( var i = 0; i < form.length; i++ ) {
    var e = form.elements;
    var ename = e.name;
    if ( ename != radio ) continue;
    if ( e.checked ) return e.value;
  }
  return "";
}


But why can't i just simply check the elemet's property?
Seems simpler to me.


I agree with Anthony. The best and most appropriate input type for
your purpose is 'checkbox'.
 
R

Ron Eggler

Anthony said:
Ron Eggler said:
Hi,

I would like to run a jvascript, checking if the radio button is checked.
My radio button looks like:

<input type="radio" name="owned_business" value="yes">

and i would like to check it with this:

alert (document.myform.owned_business.checked);
if (document.myform.owned_business.checked == false)

but alert returns me "undefined" and i don't understand why, my form name
is "myform" and the name of the radio is "owned_business" so i'm not
seeing where i go wrong.
Can anyone help me?

Thanks,
Ron


Try
alert(document.forms['myform']['owned_business'][0].checked);

Radios in my experience are accessed just like arrays, because the
browsers expect more than one. Incidentally, why not a checkbox?

~A!
Okay,
Cool, this works just fine!

PERFECT!

Thanks :)
Ron
 
D

David Mark

Ron Eggler said:




I would like to run a jvascript, checking if the radio button is checked..
My radio button looks like:
<input type="radio" name="owned_business" value="yes">
and i would like to check it with this:
        alert (document.myform.owned_business.checked);
        if (document.myform.owned_business.checked == false)
but alert returns me "undefined" and i don't understand why, my form name
is "myform" and the name of the radio is "owned_business" so i'm not seeing
where i go wrong.
Can anyone help me?
Thanks,
Ron

Try
alert(document.forms['myform']['owned_business'][0].checked);

Best to use:

document.forms['myform'].elements['owned_business'][0].checked

Or:

document.forms.myform.elements.owned_business[0].checked
 
S

Steve Swift

Randy said:

That's a very comprehensive reply. I presume, from the lack of
references to ID within radio buttons, that you cannot make some sort of
reference to the checked state of a specific radio button by giving them
distinct ID values?
Such as:

<INPUT TYPE=RADIO NAME=Q VALUE=1 ID=Q1>
<INPUT TYPE=RADIO NAME=Q VALUE=2 ID=Q2>
allowing the use of something like: document.form.Q1.checked.

If it were this simple, then even I could manage it.
 
A

Anthony Levensalor

Steve Swift said:
That's a very comprehensive reply. I presume, from the lack of
references to ID within radio buttons, that you cannot make some sort of
reference to the checked state of a specific radio button by giving them
distinct ID values?
Such as:

<INPUT TYPE=RADIO NAME=Q VALUE=1 ID=Q1>
<INPUT TYPE=RADIO NAME=Q VALUE=2 ID=Q2>
allowing the use of something like: document.form.Q1.checked.

If it were this simple, then even I could manage it.


You can do that.

~A!
 
D

David Mark

Steve Swift said:





You can do that.

But you wouldn't want to. Assuming a form with name "formName", the
two elements' checked values should be referenced as:

document.forms.formName.elements.Q[0].checked
document.forms.formName.elements.Q[1].checked

Those aren't any harder than the example in question and will work in
virtually any browser.
 
E

Evertjan.

David Mark wrote on 03 jan 2008 in comp.lang.javascript:
You can do that.

But you wouldn't want to. Assuming a form with name "formName", the
two elements' checked values should be referenced as:

document.forms.formName.elements.Q[0].checked
document.forms.formName.elements.Q[1].checked

But only if the input/radios are/stay in the right order!

If you, a year later, change the code to to

<INPUT TYPE=RADIO NAME=Q VALUE=2 ID=Q2> water<br>
<INPUT TYPE=RADIO NAME=Q VALUE=1 ID=Q1> fire<br>

you could have a hell of debugging.
 
S

Steve Swift

Evertjan. said:
But only if the input/radios are/stay in the right order!

If you, a year later, change the code to to

<INPUT TYPE=RADIO NAME=Q VALUE=2 ID=Q2> water<br>
<INPUT TYPE=RADIO NAME=Q VALUE=1 ID=Q1> fire<br>

you could have a hell of debugging.

This isn't a problem in my case - about 99.99% of my HTML is generated
by CGI scripts, so where I need to keep things consistent (such as the
ID of a particular radio button and the corresponding JavaScript) they
would both be generated by reference to a common variable.
 
A

Anthony Levensalor

Randy Webb said:

[snip]
IE might let you get away with that but IE doesn't have a clue what the
difference between an ID and a NAME is.


Firefox lets you get away with it, and so does Safari on Win. They treat
it kjust like any other element with an ID. I've seen a lot of people
"work around" radios this way, although I'm not a huge fan of it, it can
definitely be done across the majors.


Radio Buttons are the biggest pain in the rear end that you will ever
encounter in scripting forms.


I think they're tied for first with multiple selection select elements,
but it's close.

~A!
 
M

Matt Kruse

Radio Buttons are the biggest pain in the rear end that you will ever
encounter in scripting forms.

I agree they are a particular pain, but so is any set of inputs with
the same name - especially when your client-side code doesn't know in
advance if there is just one element with a given name or multiple.

The worst I've had to deal with is working with a form that had
different types of inputs, all with the same name but with uniquely-
identifying values. That is hell.

Matt Kruse
 
A

Anthony Levensalor

Randy Webb said:

[snip]
They substitute the ID for a NAME attribute when using the forms
collection?

No, sorry, misread his code. I thought you were saying they didn't allow
access via ID, and was confused.
 
A

Anthony Levensalor

Matt Kruse said:

[snip]
The worst I've had to deal with is working with a form that had
different types of inputs, all with the same name but with uniquely-
identifying values. That is hell.


I think this one wins the PITA award, Randy.

~A!
 

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
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top