ASP.NET - asp.net radiobutton list selected index using js

Asked By asif hameed on 31-Jul-09 03:30 PM

Hi all,

How can i get selected index of asp.net radiobutton list using javascript when it is visible. I need to check on button's click that if radio button list is visible then check its selected index and if it is 0 then call a js function else dont call it

Regards,

Asif Hameed

Use this.

[)ia6l0 iii replied to asif hameed on 31-Jul-09 03:39 PM
If your radiobuttonlist is defined as follows:    

    <asp:RadioButtonList ID="sampleRBL" runat="server" >
      <asp:ListItem Value="list1">test1</asp:ListItem>
      <asp:ListItem Value="list11">test11</asp:ListItem>
      <asp:ListItem Value="list111" Selected="True">test111</asp:ListItem>
    </asp:RadioButtonList>
   
    </div>
      <asp:Button ID="test" runat="server" Text="Button" OnClientClick="checkforchecked()" />
    </form>
</body>

Then this js would suffice.

    <script language="javascript" type="text/javascript">
      function getCheckedRadio() {
        var radioButtons = document.getElementsByName("sampleRBL");
        for (var j = 0; j < radioButtons.length; j ++) {
          if (radioButtons[j].checked) {
            alert("the listiem " + radioButtons[j].id + " is checked");
return;
          }
        }
      }
    </script>

radiobutton selectedindex

asif hameed replied to [)ia6l0 iii on 31-Jul-09 04:14 PM
how to check if radiobuttonlist is visible and available only then check its selected item

Re

Ravenet Rasaiyah replied to asif hameed on 31-Jul-09 10:07 PM
Hi

Yes  you can do this also,

First add this html code like this way.

<asp:RadioButtonList runat="server" ID="rdotest">
<asp:ListItem Enabled="true" Text="d1"></asp:ListItem>
<asp:ListItem Text="d2">
</asp:ListItem>
<asp:ListItem Text="d3"></asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="btntest" runat="server" OnClientClick="IsVisibleOnly();" />

then call this JavaScript in your head tag

<script type="text/javascript" language="javascript">



function IsVisibleOnly() {
var radioButtonscol = document.getElementsByName("rdotest");
for (var i = 0; i < radioButtonscol.length; i++) {
if (radioButtonscol[i].visible) {
if (radioButtonscol[i].selectedindex = 0) {
alert('Process here');
}
else {
return
}
}
}
}

</script>

Thank you
http://www.codegain.com