check if radio is checked

D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
, Wed, 2 Jan 2008 19:48:21, Richard Cornford
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.
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.
 
M

Matt Kruse

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/interact/forms.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
 
E

Evertjan.

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. 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!
 
E

Evertjan.

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


<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 = '';
};
 
A

Anthony Levensalor

Randy Webb said:

[snip]
I have seen benefits to Radio Buttons, never seen a benefit to the
scenario Matt is describing. I don't even use the benefits of name[]
that PHP brings to you. Although just the prospects of it sounds like hell.


See, and I'll use the first level of that in PHP, with simple arrays, or
running a couple in parallel. But when it comes time to start running
multiple subscripts/dimensions, I'm all set, no thanks.
 
A

Anthony Levensalor

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


Typically. Did you see that, E?

no way to unselect a radio button

[snip]

<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!
 
E

Evertjan.

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


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?
<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.
 
A

Anthony Levensalor

Evertjan. said:

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!
 
A

Anthony Levensalor

David Mark said:
Best to use:

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

Or:

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


Why?
 
S

Steve Swift

Evertjan. said:
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.
 
D

David Mark

David Mark wrote on 03 jan 2008 in comp.lang.javascript:
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!

Not really.
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.

Only one can be checked and the value of that checked element is all
that matters. For that reason, it doesn't make sense to reference a
specific radio button by ID. Granted, the OP seems to indicate a
desire to do so.
 
E

Evertjan.

Steve Swift wrote on 04 jan 2008 in comp.lang.javascript:
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.
 
T

Thomas 'PointedEars' Lahn

Anthony said:
David Mark said:
Best to use:

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

Or:

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

Why?

The former adheres to the W3C DOM Level 2 HTML Specification while
maintaining backwards compatibility to "DOM Level 0":

http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-26809268
http://www.quirksmode.org/js/dom0.html
http://docs.sun.com/source/816-6408-10/document.htm

Whether or not the latter also complies with W3C DOM Level 2 HTML is a
matter for debate; I say "no", because the ECMAScript-binding section of the
Specification does not define it this way; other people have said "yes" with
the rationale that both property access syntaxes are equal for identifiers
in ECMAScript implementations (ES3 Final, 11.2.1), and that the
specification of this equality is outside the scope of the W3C DOM Level 2
HTML Specification.

http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html


PointedEars
 
S

Steve Swift

Evertjan. said:
You must live in the States,
where 'divulging the truth' is such a terrible thing?

I have lived in the States, but no longer do.
My website solicits answers on sensitive topics from employees. In my
original group there was just one woman. If she had divulged her sex
then the manager would be able to identify her answers. Apart from the
fact that I always said I was female, just to add to the confusion which
every manager should have inflicted upon them IMO.

Of course, the real scenario was considerably more complex that that,
but the general principle was the same.

In a country where sexual discrimination is illegal, you're on thin ice
requiring someone to divulge theirs. And in many cases, "I prefer not to
say" is a more interesting version of 'truth'.
 
E

Evertjan.

Steve Swift wrote on 05 jan 2008 in comp.lang.javascript:
I have lived in the States, but no longer do.
My website solicits answers on sensitive topics from employees. In my
original group there was just one woman. If she had divulged her sex
then the manager would be able to identify her answers. Apart from the
fact that I always said I was female, just to add to the confusion
which every manager should have inflicted upon them IMO.

Any information can lead to discrimination.
So if you don't want information, don't ask anything.
Blind statistical information is not the only use of radio buttons,
so it should not influence the functionality of a html.
Of course, the real scenario was considerably more complex that that,
but the general principle was the same.

In a country where sexual discrimination is illegal, you're on thin
ice requiring someone to divulge theirs. And in many cases, "I prefer
not to say" is a more interesting version of 'truth'.

Asking someone for his/her name could just as easily lead to
discrimination.

What if your manager wants to plan the number of male vs female toilets
[oh sorry: bathrooms or restrooms]?
Would he need to add a number of them for the sexless?

===========================

But we were talking about preventing the user of not choosing.

So I will suggest the following nonsensical but sexless
third choice on a download website:

o I will download the file now
o Send me the file on a CD
o Don't know, you choose
__________________
| Dispatch order |
 
M

micrexp

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

Because that is the standard and most compatible syntax.

There is the best use


<input type="radio" name="r" value = "1" id="radio1" />

First ,marked the element by "id" property

Then use this expression to find it

<script language="javascript">
var myradio = document.getElementById("radio1");
alert(myradio.checked);
</script>
 
T

Thomas 'PointedEars' Lahn

David Mark said:
Best to use:
document.forms['myform'].elements['owned_business'][0].checked
Or:
document.forms.myform.elements.owned_business[0].checked
Why?
Because that is the standard and most compatible syntax.

There is the best use

No, certainly it is not.
<input type="radio" name="r" value = "1" id="radio1" />

That would be XHTML, a language that is not universally supported on the Web
as of yet.
First ,marked the element by "id" property

Then use this expression to find it

<script language="javascript">

Your markup is not even valid and you are actually talking about "best use"?
var myradio = document.getElementById("radio1");

That would require the UA to implement this method of W3C DOM Level 2+ Core
*properly*. Especially Internet Explorer is known not to do that, as we
discussed numerous times before:

http://pointedears.de/scripts/test/dom/names-and-ids
alert(myradio.checked);

The return value of the method should be tested before it is used. alert()
is a method of Window objects and should be called so -- window.alert(...).

Please attain a minimum amount of basic knowledge before you post more of
your "recommendations".


PointedEars
 
S

Steve Swift

Thomas said:
Please attain a minimum amount of basic knowledge before you post more of
your "recommendations".

Just a philosophical point: "best" and "recommendations" are both
entirely subjective and require little or even no prior knowledge.

My dog could make best recommendations, but that would usually imply the
things that smell most like sweaty feet (her favourite; there's no
accounting for taste).

I have almost no knowledge of Javascript, but still know how I'd
recommend something be done, and what I find best.
 
A

Anthony Levensalor

Thomas 'PointedEars' Lahn said:
Anthony said:
David Mark said:
Best to use:

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

Or:

document.forms.myform.elements.owned_business[0].checked
Why?

The former adheres to the W3C DOM Level 2 HTML Specification while
maintaining backwards compatibility to "DOM Level 0":

http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-26809268
http://www.quirksmode.org/js/dom0.html
http://docs.sun.com/source/816-6408-10/document.htm

Thanks, that's what I was looking for. Much obliged.

~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

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top