Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

get the selected radio button from radio button list

I have a radio button that sits inside a panel that pops up when a user needs to fill out some information.  It should work but it seems to not get at the code, don't know what wrong at this point.  i am wondering if its becuase its in an asp modal pop-up extender or if I am just not getting at control in javascript.

What I need to do is depending on which radiobutton they pick I need to unhide a dropdown list.  It all needs to be in javascript as modal pop-ups cannot post back to themselfs.


 <tr>
                <td style="width:10%"></td>
                <td style="width:80%;text-align:left"><asp:RadioButtonList ID="rblRecType" runat="server" Font-Size="Small" OnClientClick="GetListItem()"></asp:RadioButtonList></td>
                <td style="width:10%"></td>
            </tr>

 <script type="text/javascript">
             function GetListItem() {
                var rblist = document.getElementById("<%=rblRecType.ClientID%>");
                var radio = rblist.getElementsByTagName("input");
                var RecTeam = document.getElementById("<%=ddlRecTeam.ClientID%>");
                 for (var i = 0; x < radio.length; i++) {
                     if (radio.checked)
                     {
                        RecTeam.style.display = true
                     }
                }
                 
            }
       </script>
0
kdeutsch
Asked:
kdeutsch
  • 8
  • 8
1 Solution
 
dejaanbuCommented:
change these lines:
   
   if (radio.checked)
                     {
                        RecTeam.style.display = "block"; // or "inline";
                     }
0
 
leakim971PluritechnicianCommented:
test page : http://jsfiddle.net/aFYSt/

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
<script type="text/javascript">
    window.onload = function() {
        var radios = document.getElementById("<%= rblRecType.ClientID %>").getElementsByTagName("input");
	    for(var x=0;x<radios.length;x++) {
	        radios[x].onchange = function () {
	            document.getElementById("<%= ddlRecTeam.ClientID %>").style.display = this.checked?"block":"none";
			}
        }
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td style="width:10%"></td>
                <td style="width:80%;text-align:left"><asp:RadioButtonList ID="rblRecType" runat="server" Font-Size="Small"><asp:ListItem Text="one" Value="one"></asp:ListItem></asp:RadioButtonList></td>
                <td style="width:10%"></td>
            </tr>
        </table>
        <asp:TextBox ID="ddlRecTeam" runat="server" style="display:none">RECTEAM</asp:TextBox>
    </div>
    </form>
</body>
</html>

Open in new window

0
 
kdeutschAuthor Commented:
All,

Ok, neither is working for me, this is my latest code, changed my asp event to  onchange.

<asp:RadioButtonList ID="rblRecType" runat="server" Font-Size="Small" onChange="GetListItem()"></asp:RadioButtonList>

If I write an alert statment it gets into it, but when I put all my stuff back in it fails agian with no errors. What I need it to do is make visible a dropdownlist based on a certian radio button picked in the list.



  <script type="text/javascript">
             function GetListItem() {
                var rblist = document.getElementById("<%=rblRecType.ClientID%>").getElementsByTagName("input");
                var RecTeam = document.getElementById("<%=ddlRecTeam.ClientID%>");
                 for (var i = 0; x < rblist.length; i++) {
                     if (rblist.value == 1)
                     {
                         RecTeam.style.display = "inline";
                     }
                }
                 
            }
  </script>
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
kdeutschAuthor Commented:
leakim971,

tried your code outright and it works to a certian extent.  What happens is it opens but I need to click on the radio button twice to get the dropdownlist to open.  Is there a way to change to show the dropwdownlist based on a radiobutton value.
0
 
leakim971PluritechnicianCommented:
Did not see where and how you tried to use my code..
0
 
kdeutschAuthor Commented:
Did not post it but here is the code I tried.

 function GetListItem() {
                 var radios = document.getElementById("<%= rblRecType.ClientID %>").getElementsByTagName("input");
                 for (var x = 0; x < radios.length; x++) {
                     radios[x].onchange = function () {
                         document.getElementById("<%= ddlRecTeam.ClientID %>").style.display = this.checked ? "block" : "none";
              }
        }
    }
0
 
leakim971PluritechnicianCommented:
I don't have :  function GetListItem() {

In my solution, I just set onchange for all radio button inside the radio list not onchange on the radiolist itself (it's a table as you can see in the jsfiddle)

You did not get any feedback from the jsfiddle or the asp.net code
0
 
kdeutschAuthor Commented:
Hi,
ok, see what you are doing but for my situation i need to be able to get the value of the radiobutton list and show dropwdown lists or a checkbox depending on what they picked in the radiobutton list.. Everywhere I look online this code is approx the same and should work but its not working for me.

 function GetListItem() {
                 //Get the radiobuttonlist reference            
                 var radio = document.getElementsByName('<%= rblRecType.ClientID%>');                        
                 //local variable to store selectedvalue            
                 var selectedvalue;                        
                 
                 //loop through the items in radiobuttonlist            
                 for (var j = 0; j < radio.length; j++) {
                     if (radio[j].checked) alert(radio[j].checked);
                     
                 }
             }
0
 
leakim971PluritechnicianCommented:
radio[j].checked let you know if the radio is checked or not
radio[j].value give you the value of the radio button

I don't have :  function GetListItem() {
You did not get any feedback from the jsfiddle or the asp.net code
0
 
leakim971PluritechnicianCommented:
Here we easily alert the value : http://jsfiddle.net/aFYSt/1/
0
 
kdeutschAuthor Commented:
HI,
Ok knew nothing about that site, have been just writing alerts to see where i get values.  Thanks for new info.

right now as soon as I get to this part if get nothing after this. My alert will not show up.
if (radio[j].checked)

I threw it into the jsfiddle and got no values returned. but it gave me errors cuase of asp code as part of radiobutton list.  have no clue how to get past this when everysite has same to get at this control.
0
 
leakim971PluritechnicianCommented:
any link to see your page?
or just do a right click on it and choose view source to post it here
0
 
kdeutschAuthor Commented:
Sorry no link,

the view source is about 1500 lines as it includes  information from a telerik radgrid.  

I absolutely stupid thing is we got it to work with jquery with this method.

            function test() {
                alert($('#<%=RadioButtonList1.ClientID %> input[type=radio]:checked').val());
            }


So right now we are tyring tofigure out why it does not work with everyday javascript.
0
 
leakim971PluritechnicianCommented:
Yes of course... but you did not mention you've jQuery in the box...
0
 
kdeutschAuthor Commented:
HI,

we dont use it becuase nobody really knows anything about it but we found a site that referenced using it and tried it and it worked.  Still trying to get to work with just js.
0
 
leakim971PluritechnicianCommented:
The same with only pure JS :
function test() {
     var RadioButtonList1 = document.getElementById("<%=RadioButtonList1.ClientID %>");
     var radios = RadioButtonList1.getElementsByTagName("input");
     for(var i=0;i<radios.length;i++) {
         if( radios[i].checked ) {
               alert( radios[i].value );
         }
     }
}

Open in new window

0
 
kdeutschAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now