Dumb question that I'm struggling with

J

jbeall

I have a radio button, and I would like to use JS to allow it to be
unselected when you click an already selected radio button. I thought
this would be really simple. Here is what I'm doing:

<input type="radio" value="temporary" id="radioTemporary"
name="redirectType" onclick="if(this.checked==true){this.checked=false;
}" />

I expected this to uncheck the radio button if it was clicked when
already checked. However, it effectively prevents it from being
selected. Any help?

-Josh
 
M

Martin Honnen

I have a radio button, and I would like to use JS to allow it to be
unselected when you click an already selected radio button. I thought
this would be really simple. Here is what I'm doing:

<input type="radio" value="temporary" id="radioTemporary"
name="redirectType" onclick="if(this.checked==true){this.checked=false;
}" />

I expected this to uncheck the radio button if it was clicked when
already checked. However, it effectively prevents it from being
selected. Any help?

You could try
<input type="radio"
onclick="if (this.checked) {
this.checked = false;
if (event.preventDefault) {
event.preventDefault();
}
return false;
}
else {
return true;
}"
but I haven't tested that and I am afraid you might get different
results in different browsers as manipulating the checked property in an
onclick handler is tricky. And changing the behavior of standard
controls is usually not a good idea as it is going to confuse users.
 
C

Chris

Hi Josh

try this, it works for me

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>

<script type="text/javascript">

function on_off(me)
{

if(me.value=='off'){me.checked=true;me.value='on'}else{me.checked=false;me.v
alue='off'};
alert(me.checked);
}
</script>
</head>

<body>
<input type="radio" value="off" id="radioTemporary" name="redirectType"
onclick="on_off(this)" />
</body>
</html>

Chris
 

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,787
Messages
2,569,629
Members
45,332
Latest member
LeesaButts

Latest Threads

Top