Solved

get the selected radio button from radio button list

Posted on 2013-05-30
17
443 Views
Last Modified: 2013-05-31
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
Comment
Question by:kdeutsch
  • 8
  • 8
17 Comments
 
LVL 14

Expert Comment

by:dejaanbu
ID: 39208503
change these lines:
   
   if (radio.checked)
                     {
                        RecTeam.style.display = "block"; // or "inline";
                     }
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39208522
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
 

Author Comment

by:kdeutsch
ID: 39208737
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
 

Author Comment

by:kdeutsch
ID: 39208762
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39208774
Did not see where and how you tried to use my code..
0
 

Author Comment

by:kdeutsch
ID: 39208820
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39208875
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
 

Author Comment

by:kdeutsch
ID: 39208903
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 82

Expert Comment

by:leakim971
ID: 39208913
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39208925
Here we easily alert the value : http://jsfiddle.net/aFYSt/1/
0
 

Author Comment

by:kdeutsch
ID: 39208973
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39208989
any link to see your page?
or just do a right click on it and choose view source to post it here
0
 

Author Comment

by:kdeutsch
ID: 39209101
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39209107
Yes of course... but you did not mention you've jQuery in the box...
0
 

Author Comment

by:kdeutsch
ID: 39209132
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
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39209146
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
 

Author Closing Comment

by:kdeutsch
ID: 39210898
thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now