Solved

set all radio buttons to "No"

Posted on 2011-02-15
3
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
This article outlines some of the reasons why an email message gets flagged as spam on a recipient's end.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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