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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Method to clear an asp radiobuttonlist using javascript

Status
Not open for further replies.

Velocityman

Programmer
Mar 25, 2014
4
US
Using VWD 2008, (I know there are newer versions), here is the what a want to accomplish. A button that will uncheck a radiobuttonlist with only one item. All I want to accomplish in javascript. Since there is only one item in list, is there a one line of code to complete this? This should not be that difficult. What am I missing in the javascript code and/or event within the button. Any assistance would be greatly appreciated. I do not want to change the control by the way.

Thanks!

button:

<asp:Button ID="Button1" runat="server" OnClientClick="ClearMedicaidButton()" style="margin-left: 0px" Text="Clear Selection (Medicaid)" UseSubmitBehavior="False" />

Javascript:

function ClearMedicaidButton()
{
var rgrp = document.getElementsByName('RadioButtonList6');
rgrp[0].selected = false;
rgrp[0].checked = false;
}

RadioButtonList:

<asp:RadioButtonList ID="RadioButtonList6" runat="server"
TabIndex="69" Width="300" style="margin-left: 6px">
<asp:ListItem Value="Medicaid (Medical Assistance Program)">
Medicaid (Medical Assistance Program)
</asp:ListItem>
</asp:RadioButtonList>
 
you could do the following

Code:
function ClearMedicaidButton(){
var rgrp = document.getElementsByName('RadioButtonList6');
rgrp[0].checked = false;
}

this presumes that the radiobuttonchecklist is being rendered as a set of radio buttons (inputs). I do not think this is necessarily correct depending on the browser string sent to asp. for example, a radio button list might be rendered as a select option i guess.

so perhaps a more granular code is needed

Code:
function ClearMedicaidButton(){
 var rgrp = document.getElementsByName('RadioButtonList6');
 for(var i = 0; i < rgrp.length; i++){
   if(rgrp[i].type == 'radio'){
     if(rgrp[i].checked == true) rgrp[i].checked = false;
   } else { 
     if(rgrp[i].options.length){
      for(var j = 0; j < rgrp[i].options.length; j++){
        rgrp[i].options[j].selected = false;
     } 
 }
}

let us see how the control renders if none of the above work for you.
 
Thank you for your help. The method works when deleting the options.length section from the javascript code.
 
that's because there is a curly brace missing.

Code:
function ClearMedicaidButton(){
 var rgrp = document.getElementsByName('RadioButtonList6');
 for(var i = 0; i < rgrp.length; i++){
   if(rgrp[i].type == 'radio'){
     if(rgrp[i].checked == true) rgrp[i].checked = false;
   } else { 
     if(rgrp[i].options.length){
      for(var j = 0; j < rgrp[i].options.length; j++){
        rgrp[i].options[j].selected = false;
      }
     [red]}[/red] 
 }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top