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

Controlling radio buttons

Status
Not open for further replies.

Mephist0222

Programmer
May 14, 2008
12
US
I thought this would take 5 min, but 2 hours later, I am looking for help.

I have an unknown amount of radio buttons, I need to have them selected via 2 functions.

One selects them going down (++) the other going up (--). None can be selected until an action is taken. The fist one is selected no matter if you are going up or down should be the first radio button.

From me testing this out:

//function radio() {
//var x = document.otherForm.other;
//for (index=0; index < x.length; index++) {
//if (!x[index].checked) {
//x[0].checked = true;
//var radioValue = document.frmRadio.choice[index].value;
//break;
//}
// else {
//var i = x[index].checked
//x[i++].checked = true;
//var radioValue = document.frmRadio.choice[index].value;
//break;
//}
//}
//}



function radio(){
var x = document.otherForm.other;
for (var i=0;i<x.length;i++) {

if(!x.checked){
x[0].checked = true;
break;
}
else{
x[x.checked++].checked = true;
}
}
}


</script>

</head>

<body>



<form name="otherForm" method="post" onsubmit="validate('quant'); return false">
<input type="radio" name="other"> Size <br>
<input type="radio" name="other"> Color<br>
<input type="radio" name="other"> Gender
</form>

<a href="javascript:radio();" return false;>rooriro</a>
 
Which bit do you need help with? Stopping them from being selected until an action is taken or something else? If the former, what is the action that allows them to be selected?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
It should have 2 functions that shoud behave like this:

down(){
if(no radio button are checked){
check the first one
}
else {
the next radio button is checked (they all have the same name so only one can be checked) this will stop at the last one
}
}

up(){
if(no radio button are checked){
check the first one
}
else {
the previous radio button is checked (they all have the same name so only one can be checked) this will stop at the first one
}
}
 
>the next radio button is checked (they all have the same name so only one can be checked) this will stop at the last one
What is then the next of the last button in down()? Similarly, what is the next of the first button of the up()?
 
They would stop. So the function would do nothing. I just got this working, here is the code:

<html>
<script>
filter = ""
function radio(down)
{
if(document.otherForm) {
var x = document.otherForm.other;
var index = -1;
for (var i = 0; i < x.length; i++)
{
if (x.checked)
{

if (down)
{
index = i + ((i < (x.length - 1)) ? 1 : 0);
}
else
{
index = i - ((i > 0) ? 1 : 0);
}
break;

}
}
x[((index == -1) ? 0 : index)].checked = true;
filter = x[((index == -1) ? 0 : index)].value;
}
}
function goFilter() {
if (filter != "") {
window.location.href=filter;
}
}
</script>
<body>
<form name="otherForm" method="post" onsubmit="goFilter(); return false">
<input type="radio" name="other" value=" Size <br>
<input type="radio" name="other" value=" Color<br>
<input type="radio" name="other" value=" Gender
<input type=submit>
</form>

<a href="javascript:radio(true);">Down</a>
<a href="javascript:radio(false);">Up</a>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top