Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
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 6-Jun-11 23:04pm
Edited 7-Jun-11 2:32am
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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'); });
        });
  Permalink  
v4
Comments
beginner in C#.net at 7-Jun-11 4:10am
   
on button click OnClientClick="as()";
RakeshMeena at 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 at 7-Jun-11 4:58am
   
Spot On
beginner in C#.net at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Could you try this..
<script type="text/javascript">
    function as()
    {
        if(!document.getElementById('<%=RadioBtn.ClientID %>_0').checked)
        {
        alert("check the field");
        }
    }
    </script>
 
<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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

The problem is the HTML code generated for ASP.NET code
<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
<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
  Permalink  
v2
Comments
beginner in C#.net at 7-Jun-11 7:26am
   
it shows error as
 
The name 'RadioBtn' does not exist in the current context
Hemant-Sharma-81 at 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"?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hi its very easy
 
try this i hope it works i have tried
 
    <input id="Radio1" type="radio"  önclick="return alert('Hi friends i m sachin here');" />
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
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)
}
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Comments
Hemant-Sharma-81 at 7-Jun-11 7:02am
   
Why did you copy-paste my solution ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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;
                  }
              }
          }
//Check if any radio button is checked
       if(flagRadioButtonChecked)
       {
           alert(radioButton.value + ' is checked...');
       }
   }
   </script>
 

its working in my application
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

<script type="text/javascript">
function as()
{
var checked = document.getElementById("RadioBtn").checked);
if(!checked){
alert("check the field");
}
</script>
  Permalink  
Comments
CHill60 at 26-Feb-14 11:18am
   
There were already 7 solutions to this question - it's over 2 years old!
Ronald.A at 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
0 OriginalGriff 505
1 Maciej Los 325
2 Richard MacCutchan 265
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 26 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100