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
Solved

set all radio buttons to "No"

Posted on 2011-02-15
3
306 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to run jar file 4 151
js file for one script block only 4 149
CGI script doesn't wait for shell script to be executed 3 184
add a button to a gridview in JavaScript, 4 65
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
ConnectWise and their customers need to ensure critical alerts automatically reach the right person at the right time. MSP superheros efficiently respond to these alerts key is providing automatic, intelligent alerting that generates a complete audi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

790 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