Click here to Skip to main content
15,846,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends i m having radio button. When i uncheck radio button, alert will come but for me alert coming even i checked radio button

my code is
<script type="text/javascript">
function as()
{
<code>//if(!document.getElementById("RadioBtn").checked)
</code>{
alert("check the field");
}
}
</script>

and in div
<asp:RadioButtonList ID="RadioBtn" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
<asp:button Onclientclick="as()" runat="server">


i want to use validation using javascript.
Posted
Updated 5-Mar-23 19:47pm
v5

Where you are assigning the function to radiobuttonlist's event?
Further I don't think RadionButtonList has a "Checked" property or even an attribute.

Try this JQuery code to get an alert when you select (since deselect is done automatically once you select some other radio button in the same RadioButtonList):
$(document).ready(function() {
            $("#RadioBtn input[type='radio']").click(function() { alert('hi'); });
        });
 
Share this answer
 
v4
Comments
beginner in C#.net 7-Jun-11 4:10am    
on button click OnClientClick="as()";
RakeshMeena 7-Jun-11 4:18am    
Got your problem..... it's all messed up.... There are multiple problems:
1. "document.getElementById("RadioBtn").checked" is undefined as I said there is no property like this with RadioButtonList.
2. Your original questions says that you want alert when the uncheck is made but how a button is serving this purpose?
Ashishmau 7-Jun-11 4:58am    
Spot On
beginner in C#.net 7-Jun-11 7:30am    
here i want to use validation using javascript for radiobutton if its selected proceed otherwise create alert.. thats my issue
RakeshMeena 7-Jun-11 7:57am    
I believe you want to check as to which radio button is selected (to decide the validation).
You can below piece of JQuery to get all the selected radio buttons under a RadioButtonList: "$("#RadioBtn input[type='radio']:checked")".
You can query value property of each of these radio buttons for your logic.
Could you try this..
JavaScript
<script type="text/javascript">
    function as()
    {
        if(!document.getElementById('<%=RadioBtn.ClientID %>_0').checked)
        {
        alert("check the field");
        }
    }
    </script>


ASP.NET
<asp:radiobuttonlist id="RadioBtn" runat="server" repeatdirection="Horizontal" xmlns:asp="#unknown">
            <asp:listitem>Male</asp:listitem>
            <asp:listitem>Female</asp:listitem>
        </asp:radiobuttonlist>
        
        <asp:button id="Button1" runat="server" text="Button" onclientclick="as();" xmlns:asp="#unknown" />


Thanks
 
Share this answer
 
The problem is the HTML code generated for ASP.NET code
XML
<table id="RadioBtn" border="0">
            <tr>
                <td>
                    <input id="RadioBtn_0" type="radio" name="RadioBtn" value="Male" /><label for="RadioBtn_0">Male</label>
                </td>
                <td>
                    <input id="RadioBtn_1" type="radio" name="RadioBtn" value="Female" /><label for="RadioBtn_1">Female</label>
                </td>
            </tr>
        </table>
        <input type="submit" name="Button1" value="" onclick="as();" id="Submit1" />


you see everything in ASP.NET will be rendered to HTML for client and you will get above. if the above RadioButtonList control is inside some other control or you have master page the ID of the table will be different and when you are trying to document.getElementById(...) it results in undefined.

Always make sure your javascript is aligned with the client code generated for ASP.net code. you can do right click > view source in web browser.

To solve it replace your javascript code with below code
C#
<script type="text/javascript">
    function as()
    {
        //Get the client ID of the control
        var tableID = '<%= RadioBtn.ClientID %>';
        var radioButtons = document.getElementsByName(tableID)
        var totalRows = radioButtons.length;
        var radioButton;
        var flagRadioButtonChecked = false;
        for(var rowIndex=0; rowIndex< totalRows; rowIndex++)
        {
            radioButton = radioButtons[rowIndex]
            //Check if it's a radio button
            if(radioButton.type == 'radio')
            {
                //Check if it's checked
                if(radioButton.checked)
                {
                    //current radio button is checked
                    flagRadioButtonChecked=true;
                    //breaking from here will keep reference of checked radio button to radioButton variable
                    break;
                }
            }
        }
        //Check if any radio button is checked
        if(flagRadioButtonChecked)
        {
            alert(radioButton.value + ' is checked...');
        }
    }
    </script>


Hope this will help.

Thanks,
Hemant
 
Share this answer
 
v2
Comments
beginner in C#.net 7-Jun-11 7:26am    
it shows error as

The name 'RadioBtn' does not exist in the current context
Hemant__Sharma 7-Jun-11 7:49am    
do you have your javascript in the same .aspx page where you have RadioButtonList control?
is ID of RadioButtonList control is "RadioBtn"?
Hi its very easy

try this i hope it works i have tried

HTML
<input id="Radio1" type="radio"  önclick="return alert('Hi friends i m sachin here');" />
 
Share this answer
 
v3
Radio buttons are closely related to check boxes. Only, with a radio button you get just the one choice from a list of options. With a check box you can tick all the boxes, if you wanted.
<Input type = radio Name = r1 Value = "NE">North East
<Input type = radio Name = r1 Value = "NW">North West
<Input type = radio Name = r1 Value = "SE">South East
<Input type = radio Name = r1 Value = "SW">South West
<Input type = radio Name = r1 Value = "midlands">Midlands



You can play with radio buttons using javascript as belows.

C#
function GetSelectedItem() {
chosen = ""
len = document.f1.r1.length
for (i = 0; i <len; i++) {
if (document.f1.r1[i].checked) {
chosen = document.f1.r1[i].value
}
}
if (chosen == "") {
alert("No Location Chosen")
}
else {
alert(chosen)
}
}
 
Share this answer
 
Use Javascript to check whether the Radiobutton is selected or not:

<script type="text/javascript">
    function as()
    {
        //Get the client ID of the control
        var tableID = '<%= RadioBtn.ClientID %>';
        var radioButtons = document.getElementsByName(tableID)
        var totalRows = radioButtons.length;
        var radioButton;
        var flagRadioButtonChecked = false;
        for(var rowIndex=0; rowIndex< totalRows; rowIndex++)
        {
            radioButton = radioButtons[rowIndex]
            //Check if it's a radio button
            if(radioButton.type == 'radio')
            {
                //Check if it's checked
                if(radioButton.checked)
                {
                    //current radio button is checked
                    flagRadioButtonChecked=true;
                    //breaking from here will keep reference of checked radio button to radioButton variable
                    break;
                }
            }
        }
        //Check if any radio button is checked
        if(flagRadioButtonChecked)
        {
            alert(radioButton.value + ' is checked...');
        }
    }
    </script>

Hope this will solve your problem.
 
Share this answer
 
Comments
Hemant__Sharma 7-Jun-11 7:02am    
Why did you copy-paste my solution ?
C#
function radiobuttonchecking( ) {
          var tableID ='<%= RadioBtn.ClientID %>';

          var radioButtons = $(tableID).find(':input')
          var totalRows = radioButtons.length;
          var radioButton;
          var flagRadioButtonChecked = false;
          for (var rowIndex = 0; rowIndex < totalRows; rowIndex++) {
              radioButton = radioButtons[rowIndex]
              //Check if it's a radio button
              if (radioButton.type == 'radio') {
                  //Check if it's checked
                  if (radioButton.checked) {
                      //current radio button is checked
                      flagRadioButtonChecked = true;
                      //breaking from here will keep reference of checked radio button to radioButton variable
                      break;
                  }
              }
          }

XML
//Check if any radio button is checked
       if(flagRadioButtonChecked)
       {
           alert(radioButton.value + ' is checked...');
       }
   }
   </script>



its working in my application
 
Share this answer
 
v2
<script type="text/javascript">
function as()
{
var checked = document.getElementById("RadioBtn").checked);
if(!checked){
alert("check the field");
}
</script>
 
Share this answer
 
Comments
CHill60 26-Feb-14 11:18am    
There were already 7 solutions to this question - it's over 2 years old!
Ronald.A 26-Feb-14 12:54pm    
This is not intended for the user who posted the question, i know its over 2 years old, but i landed in this page when i was finding a solution, and i posted the shortest solution, for some one else who would be looking for a solution for this kind of problem.

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

  Print Answers RSS


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