Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loop through a RadioButtonList using javascript, in order to validate if a selection has been made.

Posted on 2007-11-20
4
Medium Priority
?
3,678 Views
Last Modified: 2008-02-01
Hi,

I have a radiobuttonlist which I have to validate using javascript. I know there are a million better ways and easier ways to do this using server side code, however specific to the application I am working on I need to do this in javascript.

Having searched and found many "Solutions" to this problem, I have had none that have worked.
I realy would appreciate some feedback on the easiest way to do this.
All I need to do us check if an item has been selected or not.

Cheers

0
Comment
Question by:jazz__man
[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
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
gops1 earned 1000 total points
ID: 20318780
Is this you are looking for?
<html>
	<head>
		<title>Script Demo Gops</title>
		<script type="text/javascript">
			function checkRadio(f){
				var r=f.radbtn;
				var f=false;
				for(var i=0;i<r.length;i++){
					if(r[i].checked){f=true;break}
				}
				if(!f) alert("Nothing is checked"); else alert("Checked!!!");
			}
		</script>
	</head>
<body>
	<form name="rad">
		<input type="radio" name="radbtn"><br>
		<input type="radio" name="radbtn"><br>
		<input type="radio" name="radbtn"><br>
		<input type="radio" name="radbtn"><br>
		<input type="radio" name="radbtn"><br>
		<input type="radio" name="radbtn"><br>
		<input type="radio" name="radbtn"><br>
		<input type="radio" name="radbtn"><br>
		<input type="radio" name="radbtn"><br>
		<input type="radio" name="radbtn"><br>
		<input type="button" value="Check" onclick="checkRadio(this.form)">
	</form>
</body>
</html>

Open in new window

0
 
LVL 12

Assisted Solution

by:Munawar Hussain
Munawar Hussain earned 1000 total points
ID: 20318839
here is working code for what you have asked...

in ASPX form
In head section
<script type ="text/javascript"  language =javascript >
    function LoopThroughRDList()
    {
    var MyRDList =document.form1.getElementsByTagName('input');
   
    var i=0;
        for(i=0; i < MyRDList.length - 1; i++)
        {
            if(MyRDList[i].id.indexOf("MyRadioButtonList") != -1)
            {
                if(MyRDList[i].checked)
                {
                alert("checked");
                }
                else
                {
                alert("not checked");
                }
            }
        }
    }
    </script>

IN THE FORM SECTION

 <asp:RadioButtonList ID="MyRadioButtonList" runat="server">
            <asp:ListItem>mm</asp:ListItem>
            <asp:ListItem>nn</asp:ListItem>
            <asp:ListItem>hh</asp:ListItem>
            <asp:ListItem>gg</asp:ListItem>
            <asp:ListItem>ee</asp:ListItem>
            <asp:ListItem>oo</asp:ListItem>
        </asp:RadioButtonList></div>
        <asp:Button ID="Button1" OnClientClick="LoopThroughRDList()"  runat="server" Text="Button" />


0
 
LVL 2

Author Comment

by:jazz__man
ID: 20318858
But for a RadioButtonList the Length is always 1 or at least when debugging thats what it always shows. I have 10 Items that are being pulled from the database and are being bound to each button.

So the first thing I have to do is count number of buttons on the RadioButtonList.
0
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20319217
if you are talking about counting in Javascript then the "RadioButtonList" dont hold any collection of its items .. you need to get all input type controls and then checking one by one if it is input then is checkbox, if yes then does it contains id of the RadioButtonList if yes then it is an item of the RadioButtonList
..
if you are talking about length in code serverside then you need to check
MyRadioButtonList.Items.Count  must give you the total intems in the list?
thanks
0

Featured Post

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

609 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