Forum Moderators: open

Message Too Old, No Replies

Retaining Radio button selection

         

Balaji1980

1:32 am on Feb 16, 2011 (gmt 0)

10+ Year Member



Hi,

Sorry if I am repeating the question again..! I did a search in this forum but could not find the exact answer.

Here is my question. I have 2 radio buttons with default selection. What I am doing is when a user clicks on "Close" in the second radio button, the following should happen.

1. It should check if the first radio button has a "Yes" selected else it should prompt the user that they can close only if Yes is selected.

2. Retain the old selection in the second radio button (it can be either Draft, Pending or Cancel).

I was able to accomplish step 1 but not 2. Below is my code:

<html>
<head>
<title> Radio </title>
<script type="text/javascript">

function check()
{
if ( (document.forms[0].one[1].checked == true) && (document.forms[0].two[2].checked == true) )
{
alert("You can close only when criteria is Yes");
return false;
}

}

</script>
</head>

<body>
<b>Criteria Met : </b>
<input type = radio name = one value = "Yes">Yes</input>
<input type = radio name = one value = "No" checked>No</input>
<br><br>
<b>Status :</b>
<input type = radio name = two value = "Draft" onclick=check() checked>Draft</input>
<input type = radio name = two value = "Pending" onclick=check()>Pending</input>
<input type = radio name = two value = "Close" onclick=check()>Close</input>
<input type = radio name = two value = "Cancel" onclick=check()>Cancel</input>
</body>

<html>



Also, the form I have has lot of these 2 radio button sets so I cannot temporarily store all the values in form onload event..!

Your help is greatly appreciated. Thanks in advance,

Balaji.

daveVk

5:49 am on Feb 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try

<input type="radio" name="two" value="Close" onclick="return check();">Close</input>

so that if false returned from check it is also returned to onclick

Do you need to call check() in other 3 cases ?

Balaji1980

5:58 pm on Feb 16, 2011 (gmt 0)

10+ Year Member



Hi Dave, thanks for your reply. I did as per your suggesstion but the problem is it is clearing out the selection for all the radio buttons and I want to retain the previous selection..!

daveVk

10:07 pm on Feb 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Better to remove close option when not allowed ?

function fix(el) {
var closeEl = document.forms[0].two[2];
if ( el.value = "No" ) { closeEl.style.display = "none"; }
else { closeEl.style.display = ""; }
}


<input type="radio" name="one" value="Yes" onclick="fix(this);">Yes</input>
<input type="radio" name="one" value="No" onclick="fix(this);"checked>No</input>