Current location: Hot Scripts Forums » General Web Coding » JavaScript » check if a radio button has been checked


check if a radio button has been checked

Reply
  #1 (permalink)  
Old 10-13-05, 10:59 AM
crmpicco's Avatar
crmpicco crmpicco is offline
Wannabe Coder
 
Join Date: Jan 2005
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
Talking check if a radio button has been checked

I have a problem with my JavaScript validation.

In my JavaScript function i am trying to check if a radio button has been checked
before more code is ran.

My problem only appears when there is more than one adult.

This is my JS code if there are MORE than one adult:

Code:
kostenzahler = 0;

for (counter = 0; counter <= form.pass.length; counter++)
		{
			alert("CONTROL loop = " + counter);	
			if (document.forms["form"].elements["pass"].checked)
			{
				radio_choice = true;
				kostenzahler = kostenzahler + 1;
			}
		}
This code works when there is only ONE ADULT:
Code:
if (document.forms["form"].elements["pass"].checked)
		{
			alert("UBERPRUFT!")
			radio_choice = true;
		}

This is my server-side code (ASP):
(noa = 2 for example)
Code:
	<% for x = 1 to noa %>
	<td id="ADTcell:<%=x%>">
		<%
		if CInt(x) <= CInt(noa) then
		%>	
		<input type="radio" name="pass" id="adt<%=x%>" onMouseOver="style.cursor='hand'"/>
		<%
		else
			response.Write "&nbsp;"
		end if
		%>
	</td>
	<% next %>
Reply With Quote
  #2 (permalink)  
Old 10-17-05, 03:17 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
The easiest way to check which radiobox has been checked is to use the document.getElementsByName method and loop through its children like this:
Code:
boxes=document.getElementsByName("pass")
checkedBox=0
for(var i=0;i<boxes.length;i++){
  if(boxes[i].checked){
   checkedBox=i
   break // No need to check the rest since only one can be checked.
  }  
}
alert(checkedBox)
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Radio button with pop-up when selected ? ajs JavaScript 2 12-24-04 03:39 PM
Check box and radio buttons enabling/disabling other elements El Barto Script Requests 2 12-21-04 02:40 PM
Check boxto enable/disable a button El Barto Everything Java 4 11-19-04 04:56 PM
radio button value as a path? mivec PHP 2 03-24-04 05:33 AM
radio button selection TheLaughingBandit ASP 3 08-31-03 06:22 PM


All times are GMT -5. The time now is 10:58 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.