Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Radio Button Help....when just one it wont select

Status
Not open for further replies.

PBE3

Programmer
Aug 16, 2007
25
US
Hello all! I am having a bit of trouble with radio buttons. I have a form in which populates records from a table and then are displayed on the next page with radio buttons, which allows the user to select an row to edit. Now in my javascript to find out which radio button is selected only works if theres more than one radio button. If only one is populated and the user selects it, it says none is selected.
My code:
<script language="JavaScript">

function radio_button_checker()
{
// set var radio_choice to false
var radio_choice = false;

// Loop from zero to the one minus the number of radio button selections
if (document.output.selected.length)
for (counter = 0; counter < document.output.selected.length; counter++)
{
// If a radio button has been selected it will return true (If not it will return false)
if (document.output.selected[counter].checked)
{
radio_choice = true;
break;
}
}
if (!radio_choice)
{
// If there were no selections made display an alert box
alert("Please Select a Record to Edit.");
return (false);
}
document.location = "EditRecord.jsp?index=" + counter;
}
</script>





Any suggestions?
Thanks in advance!!
 
It may have something to do with how your function is called. Also in IE if your looping document.output.selected.length
and only have one node(radiobutton) the object will not have the ".length" property. Try alerting "document.output.checked" when you only have one radio button.


 
Slow morning at work hopefully this example will help.
Code:
<html>
<head>

<script language="JavaScript">

function radio_button_checker(){
	var nodes = document.forms["myform"].output;
	if(nodes.checked){
				alert("put your function here");
				return;
	}
	else{
		for (i = 0; i < nodes.length; i++){
			if(nodes[i].checked){
				alert("put your function here");
				return;
			}
		}
	}
	alert("Please Select a Record to Edit.");
	document.location = "EditRecord.jsp?index=" + counter;
}
</script>

</head>

<body onload="">
	<form id="myform">
	1:<input type="radio" id="rid1" onclick="radio_button_checker()" name="output" value="1"><br>

	</form>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top