Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Hi huys how can i get the selected index of a radiobuttonlist or how i can check if an item in the radiobutton list is actually checked.
Thanks in advance guys.
Posted

You need to give your items ids so you can select them, then you can check which one is checked. A radiobutton list is just a lot of radio buttons, it's not a single control.
 
Share this answer
 
Comments
Member 7871170 28-Jun-11 4:38am    
i did that: var rdbMerchQ1 = document.getElementById('<%=rdbMerchQ1.ClientID %>');

then if i try rdbMerchQ1[0].checked for instance i get undefined
You'll need to access the group (name attribute) and then look for the values:

HTML
<form name="frm1">
 <input type="radio" name="sex" value="male" checked="checked" /> Male<br />
 <input type="radio" name="sex" value="female" /> Female
</form>


JavaScript
// Get the HTMLCollection: getElementsByName
var sex1 = document.getElementsByName('sex');
// or like this
var sex2 = document.forms["frm1"]["sex"];

if (!sex1[0].checked && !sex1[1].checked)
{
 alert('Nothing selected.');
}


As far as I know there is no selectedindex, so you'll need to loop through the collection.
 
Share this answer
 
You can use JQuery to do this. Below is the complete markup I tried:

<script type="text/javascript" language="javascript" src="jquery-1.6.1.js"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
<script language="javascript" type="text/javascript">
    $(document).ready(function() {
        $("#btn").click(function() {
            var selectedIndex = -1;
            $("#rbtnlist input").each(function(index) {
                if ($(this).attr('checked')) {
                    selectedIndex = index;
                    return false;
                }
            });
            alert(selectedIndex);
        });
    });
</script>
</head>
<body>
    <form id="form1"  runat="server">
        <div> 
            <asp:radiobuttonlist id="rbtnlist" runat="server" xmlns:asp="#unknown">
                <asp:listitem text="one" value="1">
                <asp:listitem text="two" value="2">
                <asp:listitem text="three" value="3">
            
        </div>
        <input type="button" id="btn" value="Get Index" />
    </form>    
</body>
</html>


It worked for me and hope it works for you as well.
 
Share this answer
 
v2
Comments
RakeshMeena 30-Jun-11 0:44am    
Updated to break out of each loop when required index is found.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900