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   
Richard Cornford
 
Posts: n/a

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






Ron Eggler 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"
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.

Quote:
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".

Quote:
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.



Reply With Quote
  #2  
Old   
Dr J R Stockton
 
Posts: n/a

Default Re: check if radio is checked - 01-03-2008 , 11:24 AM






In comp.lang.javascript message <flgppu$pcm$1$8300dec7 (AT) news (DOT) demon.co.uk>
, Wed, 2 Jan 2008 19:48:21, Richard Cornford
<Richard (AT) litotes (DOT) demon.co.uk> posted:

Quote:
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.
There is no requirement that one of them be selected initially, and it
can be appropriate to require a nominally-conscious choice rather than
providing a default.

Quote:
An single input element that is expected to have one of two boolean
states is best represented with a 'checkbox' element.
If that means that there is something naturally yes/no or on/off about
the states, then yes; but otherwise a pair of checkbuttons can make more
sense to the user. Consider the species selector in a record in a
database of cats'n'dogs.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.


Reply With Quote
  #3  
Old   
Matt Kruse
 
Posts: n/a

Default Re: check if radio is checked - 01-03-2008 , 12:37 PM



On Jan 3, 11:24 am, Dr J R Stockton <j... (AT) merlyn (DOT) demon.co.uk> wrote:
Quote:
There is no requirement that one of them be selected initially, and it
can be appropriate to require a nominally-conscious choice rather than
providing a default.
http://www.w3.org/TR/REC-html40/inte...rms.html#radio
'Since user agent behavior differs, authors should ensure that in each
set of radio buttons that one is initially "on".'

Since there is typically no way to unselect a radio button group so
that no options are checked, providing a default state that cannot be
returned to is bad design. All radio button groups should have a
default selected option.

Matt Kruse


Reply With Quote
  #4  
Old   
Evertjan.
 
Posts: n/a

Default Re: check if radio is checked - 01-03-2008 , 01:10 PM



Matt Kruse wrote on 03 jan 2008 in comp.lang.javascript:

Quote:
Since there is typically no way to unselect a radio button group so
that no options are checked, providing a default state that cannot be
returned to is bad design. All radio button groups should have a
default selected option.
Why?

One could say not being able
to return to an unselected state by javascript command
is bad.

However it escapes me why the consequence is
that the unseleceted state is not be used at the page start.

It comes very handy sometimes, like asking for m/f sex. Should we add a
default radio button named "yet unknown", that is programmaticly disallowed
to return to?

I don't think so!

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #5  
Old   
Evertjan.
 
Posts: n/a

Default Re: check if radio is checked - 01-03-2008 , 01:36 PM



Evertjan. wrote on 03 jan 2008 in comp.lang.javascript:

Quote:
Matt Kruse wrote on 03 jan 2008 in comp.lang.javascript:

Since there is typically no way to unselect a radio button group so
that no options are checked, providing a default state that cannot be
returned to is bad design.

<form>
<input type=radio name=r value = '1'>
<input type=radio name=r value = '2'>
</form>

<button onclick='deselect();'>Deselect</button>

<script type='text/javascript'>
function deselect(){
var r = document.forms[0].elements['r'];
r[0].checked = '';
r[1].checked = '';
};
</script>


Quote:
All radio button groups should have a
default selected option.

Why?



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #6  
Old   
Anthony Levensalor
 
Posts: n/a

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



Evertjan. said:

Quote:
Evertjan. wrote on 03 jan 2008 in comp.lang.javascript:

Matt Kruse wrote on 03 jan 2008 in comp.lang.javascript:

Since there is typically

Typically. Did you see that, E?

no way to unselect a radio button

[snip]


Quote:
form
input type=radio name=r value = '1'
input type=radio name=r value = '2'
/form

button onclick='deselect();'>Deselect</button

script type='text/javascript'
function deselect(){
var r = document.forms[0].elements['r'];
r[0].checked = '';
r[1].checked = '';
};
/script


Well, I'd say that you don't want radio buttons, you want round
checkboxes. And if that's what you want, make them behave that way, and
let someone click to clear them, rather than providing an extra input
people are not used to and will be confused by.

~A!

--
anthony at my pet programmer dot com


Reply With Quote
  #7  
Old   
Evertjan.
 
Posts: n/a

Default Re: check if radio is checked - 01-03-2008 , 05:44 PM



Anthony Levensalor wrote on 03 jan 2008 in comp.lang.javascript:

Quote:
Evertjan. said:

Evertjan. wrote on 03 jan 2008 in comp.lang.javascript:

Matt Kruse wrote on 03 jan 2008 in comp.lang.javascript:

Since there is typically


Typically. Did you see that, E?

no way to unselect a radio button

[snip]
You are wrong, as I showed below,
or would you say my code is atypical?

Quote:
form
input type=radio name=r value = '1'
input type=radio name=r value = '2'
/form

button onclick='deselect();'>Deselect</button

script type='text/javascript'
function deselect(){
var r = document.forms[0].elements['r'];
r[0].checked = '';
r[1].checked = '';
};
/script



Well, I'd say that you don't want radio buttons, you want round
checkboxes. And if that's what you want, make them behave that way, and
let someone click to clear them, rather than providing an extra input
people are not used to and will be confused by.
I don't, Anthony, it is the OP that wants something.

Please address your comments to the right poster.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #8  
Old   
Anthony Levensalor
 
Posts: n/a

Default Re: check if radio is checked - 01-03-2008 , 06:11 PM



Evertjan. said:


Quote:
I don't, Anthony, it is the OP that wants something.

Please address your comments to the right poster.


I hear you, and yet I see you posting a solution that would make the UI
less clear to normal everyday users.

~A!


--
anthony at my pet programmer dot com


Reply With Quote
  #9  
Old   
Steve Swift
 
Posts: n/a

Default Re: check if radio is checked - 01-03-2008 , 09:30 PM



Evertjan. wrote:
Quote:
It comes very handy sometimes, like asking for m/f sex. Should we add a
default radio button named "yet unknown", that is programmaticly disallowed
to return to?
The M/F choice is a very interesting one. If you have just the two
buttons, with neither selected initially then you risk annoying the user
considerably. They may not wish to divulge their sex, but if they
accidentally click one of the the buttons they are left with the
unpalatable choice between divulging the truth or lying. Many people
will simply abandon your page at this point, so you lose.

To avoid this on my survey websites, I always add the extra button "I
prefer not to say" although (where I know my audience well) I sometimes
label the third choice "Not in living memory" in order to raise a smile.

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk


Reply With Quote
  #10  
Old   
Evertjan.
 
Posts: n/a

Default Re: check if radio is checked - 01-04-2008 , 04:52 AM



Steve Swift wrote on 04 jan 2008 in comp.lang.javascript:

Quote:
Evertjan. wrote:
It comes very handy sometimes, like asking for m/f sex. Should we add
a default radio button named "yet unknown", that is programmaticly
disallowed to return to?

The M/F choice is a very interesting one. If you have just the two
buttons, with neither selected initially then you risk annoying the
user considerably. They may not wish to divulge their sex, but if
they accidentally click one of the the buttons they are left with the
unpalatable choice between divulging the truth or lying. Many people
will simply abandon your page at this point, so you lose.

To avoid this on my survey websites, I always add the extra button "I
prefer not to say" although (where I know my audience well) I
sometimes label the third choice "Not in living memory" in order to
raise a smile.
You must live in the States,
where 'divulging the truth' is such a terrible thing?

If my users don't want to give information I want,
good riddance. I am the boss of my webpage.

However all this had nothing to do with the possibility of giving the
user a choice, [that is what radio buttons are about], but not
influencing them by setting a preset choice for them.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


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.