Solved

set all radio buttons to "No"

Posted on 2011-02-15
3
302 Views
Last Modified: 2012-05-11
hi,

i have 5 radiobuttonlists - each containing a "Yes" and "No" option.

I can only have 1 "Yes" across all 5 lists, So when the user selects "Yes" in one of the lists - i want to reset all of the other Lists to "No".

I have the following code

//my click function on the 1st list
 $('#<%= radioButtonList1.ClientID %>').click(function() {

//if the click is a Yes
if ($('#<%= radioButtonList1.ClientID %>').find('input:radio:checked').val() == 1)
{
//then reset all other lists options to "NO" - (which is "0")
}
});

thanks


 
0
Comment
Question by:MrKevorkian
3 Comments
 
LVL 13

Assisted Solution

by:devlab2012
devlab2012 earned 250 total points
ID: 34898737
Here is the complete working code of a .NET page:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script>
     function validate(sender)
     {
        var isYes = sender.value;
        if (isYes == "1")
        {
            var radioObj = document.getElementsByTagName("input");
            var radioLength = radioObj.length;
            for(var i = 0; i < radioLength; i++) 
            {
                if(radioObj[i].type == "radio")
                {
                    if (radioObj[i].name == sender.name)
                    {
                        continue;
                    }
                    if (radioObj[i].value == "0")
                    {
                        radioObj[i].checked = "checked";
                    }
                }
            }
         }
     }
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        List 1:
        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
        <asp:ListItem Text="No" Value="0"></asp:ListItem>
        <asp:ListItem Text="Yes" Value="1" onclick="validate(this);"></asp:ListItem>
        </asp:RadioButtonList>
        <br />
        List 2:
        <asp:RadioButtonList ID="RadioButtonList2" runat="server">
        <asp:ListItem Text="No" Value="0"></asp:ListItem>
        <asp:ListItem Text="Yes" Value="1" onclick="validate(this);"></asp:ListItem>
        </asp:RadioButtonList>
        List 3:
        <asp:RadioButtonList ID="RadioButtonList3" runat="server">
        <asp:ListItem Text="No" Value="0"></asp:ListItem>
        <asp:ListItem Text="Yes" Value="1" onclick="validate(this);"></asp:ListItem>
        </asp:RadioButtonList>
        <br />
        List 4:
        <asp:RadioButtonList ID="RadioButtonList4" runat="server">
        <asp:ListItem Text="No" Value="0"></asp:ListItem>
        <asp:ListItem Text="Yes" Value="1" onclick="validate(this);"></asp:ListItem>
        </asp:RadioButtonList>
        List 5:
        <asp:RadioButtonList ID="RadioButtonList5" runat="server">
        <asp:ListItem Text="No" Value="0"></asp:ListItem>
        <asp:ListItem Text="Yes" Value="1" onclick="validate(this);"></asp:ListItem>
        </asp:RadioButtonList>
        <br />

    </div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 34898759
Check this :

"Yes" radio button have value "1", "No" have "0"
You need to put the rblist in a div :
<div id="radioButtonsInside">

Open in new window

<%@ Page Title="Page d'accueil" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $("input[type='radio'][value='1']", "#radioButtonsInside").click(function () {
            $("input[type='radio'][value='0']", "#radioButtonsInside").not( $(this).parents("table").find("input") ).attr("checked", "true");
        });
    });
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div id="radioButtonsInside">
    <asp:RadioButtonList ID="RadioButtonList1" runat="server"><asp:ListItem Text="Yes" Value="1"></asp:ListItem><asp:ListItem Text="No" Value="0" Selected></asp:ListItem></asp:RadioButtonList>
    <asp:RadioButtonList ID="RadioButtonList2" runat="server"><asp:ListItem Text="Yes" Value="1"></asp:ListItem><asp:ListItem Text="No" Value="0" Selected></asp:ListItem></asp:RadioButtonList>
    <asp:RadioButtonList ID="RadioButtonList3" runat="server"><asp:ListItem Text="Yes" Value="1"></asp:ListItem><asp:ListItem Text="No" Value="0" Selected></asp:ListItem></asp:RadioButtonList>
    <asp:RadioButtonList ID="RadioButtonList4" runat="server"><asp:ListItem Text="Yes" Value="1"></asp:ListItem><asp:ListItem Text="No" Value="0" Selected></asp:ListItem></asp:RadioButtonList>
</div>
</asp:Content>

Open in new window

leakim971-409884.flv
0
 
LVL 1

Author Closing Comment

by:MrKevorkian
ID: 34918251
thanks very much guys, both excellent!
i like the video especially!  

leakim971 edged it slightly as it was less code.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

11 Experts available now in Live!

Get 1:1 Help Now