Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unchecked RadioButtonList with Javascript 1

Status
Not open for further replies.

jmro20

Programmer
Aug 3, 2009
6
PR
I have a checkmark that when the user clicked on it if it's checked then a radiobuttonlist is visible. When the checkmark is uncheked the radiobuttonlist is not visible. What I want to do is, when the checkmark get's unchecked if any item of the radiobuttonlist is checked I wanto to unchek it, so if the user set the chekmark to checked again I want the radiobuttonlist to be visible but with all the items unchecked.

Please help, I need this rush, I have tried a lot os suggestions but with no results.

 
Its hard to say without seeing your code, but the basic idea is you check for the display status of the element hat is holding the radio buttons, and then loop throgh the radio buttons to uncheck them:

Code:
function hidethem(){
[green]\\Get the radiobutton list container[/green]
var myopts=document.getElementById("myopts");
[green]\\Check style to see what state its in: either displayed or not and act accordingly.[/green]
if((myopts.style.display=="")||(myopts.style.display=="block")){
[green]\\Set style to none to make it invisible.[/green]
myopts.style.display="none";
[green]\\at the same time get radio buttons list[/green]
var buttonGroup=document.getElementsByName('opt1');
\\loop through them to uncheck the one that may be checked. [/green]
 for (i=0; i < buttonGroup.length; i++) {
   
       if (buttonGroup[i].checked == true) { // if a button in group is checked,
             buttonGroup[i].checked = false;   // uncheck it
      }
      
   }    

}

[green]\\if the container is already invisible, just make it visible again.[/green]
else if(myopts.style.display=="none"){
myopts.style.display="block";
}

}

</script>


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
This is part of my code in my PageLoad Event (I am using a content page inside a master page)
s = New System.Text.StringBuilder

s.AppendLine("if (document.getElementsByName('" & chkCancer.ClientID.ToString & "').checked == false) {")
s.AppendLine("ClearRadioButton(document.getElementById('" & rb_MAX.ClientID.ToString & "'));}")
s.AppendLine("")

s.Append("ShowHideControl")
s.Append("(")
s.Append("document.getElementById('" & Cancer.ClientID.ToString & "')")
s.Append(",")
s.Append("document.getElementById('" & rb_MAX.ClientID.ToString & "')")
s.Append(");")

chkCancer.Attributes.Add("OnClick", s.ToString)

And these are the functions:

<script type="text/javascript">

function ShowHideControl(chk, control) {

if (chk.checked == false)
{ control.style.display = 'none'; }
else
{ control.style.display = ''; }

}

function ClearRadioButton(rbt) {

for (counter = 0; counter < rbt.length; counter++) {
if (rbt[counter].checked == true) {

rbt[counter].checked = false;
}

}

}

</script>
 
You need to stick the loop inside your if statement here:
Code:
function ShowHideControl(chk, control) {

                if (chk.checked == false)
                { control.style.display = 'none'; 
[red]\\Loop through radio buttons 
 [/red]

[gray][green]\\at the same time get radio buttons list[/green]
var buttonGroup=document.getElementsByName('[red]opt1[/red]');
[green]\\loop through them to uncheck the one that may be checked. [/green]
 for (i=0; i < buttonGroup.length; i++) {
   
       if (buttonGroup[i].checked == true) { // if a button in group is checked,
             buttonGroup[i].checked = false;   // uncheck it
      }
      
   }    [/gray]
}

Change [red]opt1[/red] to the name of your radio buttons.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
It dosen't work. Also I need to send the radiobuttonlist as a parameter to the JS function because I need to reuse that code for other checkboxes and radiobuttonlists on the content page.
 
Fine, send the radiobuttonlist as a parameter.

As for it not working, well It worked for me when I tried it.
Code:
function ShowHideControl(chk, control, [red]radiobuttonlistname [/red]) {
...
                if (chk.checked == false)
                { control.style.display = 'none';

 


var buttonGroup=document.getElementsByName([red]radiobuttonlistname[/red]);
\\loop through them to uncheck the one that may be checked.
 for (i=0; i < buttonGroup.length; i++) {
   
       if (buttonGroup[i].checked == true) { // if a button in group is checked,
             buttonGroup[i].checked = false;   // uncheck it
      }
      
   }    
}

As to why it doesn't work I cannot say. I used that same code on a test run, and it works for me as is.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I use this code and now is working, I treat the radiobuttonlist as a table that contains the radiobutton:

In the pageLoad Event I have:

dim s = New System.Text.StringBuilder
s.AppendLine("ClearRadioButton(document.getElementById('" & rb_MAX.ClientID.ToString & "'));}")
s.AppendLine("")
chkCancer.Attributes.Add("OnClick", s.ToString)

And the function in the Contant Page is:
function ClearRadioButton(rblTable) {

for (var count = 0; count < rblTable.rows.length; count++) {
if (rblTable.rows[count].cells[0].getElementsByTagName('input').length > 0) {
var rbn = rblTable.rows[count].cells[0].getElementsByTagName('input')[0];
if (rbn != null) {
if (rbn.checked == true) {
rbn.checked = false;

}
}
}
}
}

Thank for you help vacunita, i really appreciate your time!!
 
If you click the " Thank vacunita
for this valuable post!" then other people will be able to find his useful answer.

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top