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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with radio buttons 1

Status
Not open for further replies.

JimJx

Technical User
Joined
Feb 16, 2001
Messages
202
Location
US
I have a page that I am having a problem with. A very simple version doesn't seem to pass the values. I have never tried using radios before, but why is this not working?????

Complete code is:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  <title></title>
<script type="text/javascript">
<!--
function Validate(){
var Tile;
for (i = 0; i < document.form1.Tile.length; i++){
alert ("I: " + Tile[i]);
if (document.form1.Tile[i].checked == true){
Tile = document.form1.Tile[i].value; 
alert ("Tile: " + Tile);
}
}
return false;
}
//-->
</script>
</head>
<body>
<FORM name="form1">
<img height="90" src="tiles/marble.jpg" width="60" border="0"><br>
	 <input name="Tile" type="radio" value="marble">Marble
<img height="90" src="tiles/wood.jpg" width="60" align="top" border="0"><br>
	 <input name="Tile" type="radio" value="wood">Wood<p />

<p><input type="submit" onClick="Validate()"></p>
</body>
</html>

I put the alerts in there just to see what is going on, and I don't even get them to pop.

Any help greatly appreciated.
Jim
 
Put your validation function in the <form> instead, like this:

Code:
<FORM name="form1" onsubmit="return Validate();">

This line
Code:
alert ("I: " + Tile[i]);
will cause an error because the Tile variable isn't an array, and the function will terminate at that point and the form will submit without error checking. I'd guess that you're after something like this instead:
Code:
alert('I: ' + document.form1.Tile[i].checked);

Since you always return false from the function, it will never submit. You probably want to use this
Code:
if (document.form1.Tile[i].checked == true)
  {
  Tile = document.form1.Tile[i].value; 
  alert ("Tile: " + Tile);
  return true;
  }

You also might look into learning to format your code with indents to make it more readable. The few bytes you save by not indenting will save far less time than what it takes to read and figure out what goes with what and where.

Lee
 
Thanks for the help, works great.

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top