470,648 Members | 1,552 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,648 developers. It's quick & easy.

Checked radio button doesn't uncheck

Hello:

On a web page I check the value of a cookie and set one of two radio
buttons accordingly.

function setRadioForm() {
var myCookie = document.cookie;
var OpenOnly = myCookie.substring(9);
if (OpenOnly == "True") {
document.ViewOptions.Open.checked = true;
}
else {
document.ViewOptions.All.checked = true;
}
}

<body onload="setRadioForm()">
This works fine. The correct radio button appears checked when the
form opens. However, when the user checks one of the buttons (usually
the unchecked one), both buttons remain checked.

Here's the code on the form:

<form name='ViewOptions">
<input type="radio" name="Open" value="True"
onclick="setCookie("True")> Always, show me open items only.
<input type="radio" name="All" value="False"
onclick=setCookie("False")>Always show me all items, open and closed.
</form>

The onclick fires correctly, but the button which was not clicked
remains checked. Am I doing something wrong or is this a limitation
in the browser/code?

--
Ken

Jul 20 '05 #1
6 16101
When you have a group of radio buttons (I.E. only one can be selected), you
need to name them the same. The checked value, if any, will be the only one
returned under that name. Since they are named the same you need to access
them like so: 1st radio button: document.ViewOptions.Open[0].checked 2nd
radio button: document.ViewOptions.Open[1].checked

John

"Ken Loomis" <kl*****@notarealaddress.com> wrote in message
news:j0********************************@4ax.com...
Hello:

On a web page I check the value of a cookie and set one of two radio
buttons accordingly.

function setRadioForm() {
var myCookie = document.cookie;
var OpenOnly = myCookie.substring(9);
if (OpenOnly == "True") {
document.ViewOptions.Open.checked = true;
}
else {
document.ViewOptions.All.checked = true;
}
}

<body onload="setRadioForm()">
This works fine. The correct radio button appears checked when the
form opens. However, when the user checks one of the buttons (usually
the unchecked one), both buttons remain checked.

Here's the code on the form:

<form name='ViewOptions">
<input type="radio" name="Open" value="True"
onclick="setCookie("True")> Always, show me open items only.
<input type="radio" name="All" value="False"
onclick=setCookie("False")>Always show me all items, open and closed.
</form>

The onclick fires correctly, but the button which was not clicked
remains checked. Am I doing something wrong or is this a limitation
in the browser/code?

--
Ken


Jul 20 '05 #2
On Tue, 30 Dec 2003 19:48:01 -0500, Ken Loomis
<kl*****@notarealaddress.com> wrote:
Hello:

<form name='ViewOptions">
Make sure the quotes match: name="ViewOptions" or name='ViewOptions'
<input type="radio" name="Open" value="True"
onclick="setCookie("True")> Always, show me open items only.
Use a different quote character when nesting:

onclick="setCookie('True')">

....and make sure that quotes are paired.
<input type="radio" name="All" value="False"
onclick=setCookie("False")>Always show me all items, open and closed.
</form>

The onclick fires correctly, but the button which was not clicked
remains checked. Am I doing something wrong or is this a limitation
in the browser/code?


Radio buttons must belong to the same 'group' in order to have interaction
with each other. You do this by giving them the same name. Using your code
above, you could do this like so:

<FORM name="ViewOptions">
<INPUT type="radio" name="items" value="open"
onclick="setCookie('True')"> Always, show me open items only.
<INPUT type="radio" name="items" value="all"
onclick="setCookie('False')"> Always show me all items, open and
closed.
</FORM>

With this new arrangement, you might want to rename your cookies (True and
False don't quite make sense in this context).

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #3
John:

Thank you. Perfect!

Ken
On Wed, 31 Dec 2003 01:10:36 GMT, "johkar" <no********@link.net>
wrote:
When you have a group of radio buttons (I.E. only one can be selected), you
need to name them the same. The checked value, if any, will be the only one
returned under that name. Since they are named the same you need to access
them like so: 1st radio button: document.ViewOptions.Open[0].checked 2nd
radio button: document.ViewOptions.Open[1].checked

John

"Ken Loomis" <kl*****@notarealaddress.com> wrote in message
news:j0********************************@4ax.com.. .
Hello:

On a web page I check the value of a cookie and set one of two radio
buttons accordingly.

function setRadioForm() {
var myCookie = document.cookie;
var OpenOnly = myCookie.substring(9);
if (OpenOnly == "True") {
document.ViewOptions.Open.checked = true;
}
else {
document.ViewOptions.All.checked = true;
}
}

<body onload="setRadioForm()">
This works fine. The correct radio button appears checked when the
form opens. However, when the user checks one of the buttons (usually
the unchecked one), both buttons remain checked.

Here's the code on the form:

<form name='ViewOptions">
<input type="radio" name="Open" value="True"
onclick="setCookie("True")> Always, show me open items only.
<input type="radio" name="All" value="False"
onclick=setCookie("False")>Always show me all items, open and closed.
</form>

The onclick fires correctly, but the button which was not clicked
remains checked. Am I doing something wrong or is this a limitation
in the browser/code?

--
Ken



Jul 20 '05 #4
On Wed, 31 Dec 2003 01:33:00 GMT, Michael Winter
<M.******@blueyonder.co.invalid> wrote:
On Tue, 30 Dec 2003 19:48:01 -0500, Ken Loomis
<kl*****@notarealaddress.com> wrote:
Hello:

<form name='ViewOptions">
Make sure the quotes match: name="ViewOptions" or name='ViewOptions'


Michael, Thank you. The quoting in my code was correct. I transcribed
it wrong in the message.

Radio buttons must belong to the same 'group' in order to have interaction
with each other. You do this by giving them the same name. Using your code
above, you could do this like so:

<FORM name="ViewOptions">
<INPUT type="radio" name="items" value="open"
onclick="setCookie('True')"> Always, show me open items only.
<INPUT type="radio" name="items" value="all"
onclick="setCookie('False')"> Always show me all items, open and
closed.
</FORM>

I thought I had to name them differently to reference them. Thanks.
With this new arrangement, you might want to rename your cookies (True and
False don't quite make sense in this context).


Right, Thanks.

Ken

Jul 20 '05 #5
Lee
Ken Loomis said:

On Wed, 31 Dec 2003 01:33:00 GMT, Michael Winter
<M.******@blueyonder.co.invalid> wrote:
On Tue, 30 Dec 2003 19:48:01 -0500, Ken Loomis
<kl*****@notarealaddress.com> wrote:
Hello:

<form name='ViewOptions">


Make sure the quotes match: name="ViewOptions" or name='ViewOptions'


Michael, Thank you. The quoting in my code was correct. I transcribed
it wrong in the message.


So now you've learned why you should *never* transcribe code
into a post when you're asking why it doesn't work. It's
like sending your brother in to the doctor to find out why
your shoulder hurts.

Always copy and paste code that you know shows the problem.

Jul 20 '05 #6
On 31 Dec 2003 00:03:25 -0800, Lee <RE**************@cox.net> wrote:
Michael, Thank you. The quoting in my code was correct. I transcribed
it wrong in the message.


So now you've learned why you should *never* transcribe code
into a post when you're asking why it doesn't work. It's
like sending your brother in to the doctor to find out why
your shoulder hurts.

Always copy and paste code that you know shows the problem.


Right you are. The code was on a machine not corrected to newsgroups,
and I knew the problem had to be more conceptual than syntactical, but
I take your point.

Ken
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by dpazza | last post: by
3 posts views Thread by tshad | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.