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!

check of radio selection

Status
Not open for further replies.

tester321

Programmer
Mar 13, 2007
150
CA
Hi im trying to check to see if the user select "I agree" to the terms and use of the website. Two radio buttons of the same family, 1 stated i disagree the other, i agree, this is what i have which isnt working (r1 = i agree) I have the code in a "check form fields before submitting form" function, any thoughts..

if(document.all('r1').checked!=true) {
alert("You must agree to the Website Conditions to become a Member");
document.all('tou').focus(); // tou is the id of the radio that states "i disagree"
return false;
}

Thanks.
 
Hi

Please use standard references instead of [tt]all[/tt].
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title></title>
<script type="text/javascript">
function go()
{
  if (!document.getElementById('r1').checked) {
    alert('You must agree to the Website Conditions to become a Member');
    document.getElementById('tou').focus();
    return false;
  }
}
</script>
</head>
<body>
<form action="#" onsubmit="return go()">
<p>
<input type="radio" name="r1" id="r1"> I agree
<input type="radio" name="tou" id="tou" checked="checked"> I diagree
<input type="submit">
</p>
</form>
</body>
</html>

Feherke.
 
Hi

Grr... Make it :
Code:
<input type="radio" name="[red]samename[/red]" id="r1"> I agree
<input type="radio" name="[red]samename[/red]" id="tou" checked="checked"> I diagree

Feherke.
 
Actually, I think with radio buttons you need to access them with array notation like this:
Code:
  if (!document.getElementById('r1')[red][0][/red].checked) {
    alert('You must agree to the Website Conditions to become a Member');
    document.getElementById('[red]r1[/red]')[red][1][/red].focus();
    return false;
  }
because since radio buttons have the same name (for grouping) you need to reference where they are in the DOM collection - however, I could be wrong (it wouldn't be the first time).

Einstein47
There are no kangaroos in Austria!
[&#91;]Starbase47.com]
 
Hi

Einstein47, you may be right in XHTML 1.1, because there the [tt]id[/tt] completely replaced the [tt]name[/tt] attribute. But while the [tt]id[/tt] must be unique, I do not think there is any logic in having an array.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top