HighDots Forums  

Re: check if radio is checked

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: check if radio is checked in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
nolo contendere
 
Posts: n/a

Default Re: check if radio is checked - 01-02-2008 , 01:24 PM






On Jan 2, 2:09*pm, Ron Eggler <NORE... (AT) example (DOT) com> wrote:
Quote:
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[i];
var ename = e.name;
if ( ename != radio ) continue;
if ( e.checked ) return e.value;
}
return "";
}



Reply With Quote
  #2  
Old   
nolo contendere
 
Posts: n/a

Default Re: check if radio is checked - 01-02-2008 , 02:54 PM






On Jan 2, 2:45*pm, Ron Eggler <NORE... (AT) example (DOT) com> wrote:
Quote:
nolo contendere wrote:
On Jan 2, 2:09*pm, Ron Eggler <NORE... (AT) example (DOT) com> wrote:
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[i];
* * 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'.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.