dalecon
asked on
for loop checking radio button
hi all,
i have some html with radio buttuns
I'm using a javscript for loop to loop through them all to see which one is checked.
What I am finding is if there is only ONE radio buton, even though i click it the javascript isn't recognising it's checked
for (var i = 0; i < document.forms['radio'].te st.length; i++) {
if (document.forms['radio'].t est[i].che cked) {
alert ('we checked one');
return false;
}
}
so i have one radion button on a form, clicking it so it's checked butt it's not going into the loop
thanks for any help
i have some html with radio buttuns
I'm using a javscript for loop to loop through them all to see which one is checked.
What I am finding is if there is only ONE radio buton, even though i click it the javascript isn't recognising it's checked
for (var i = 0; i < document.forms['radio'].te
if (document.forms['radio'].t
alert ('we checked one');
return false;
}
}
so i have one radion button on a form, clicking it so it's checked butt it's not going into the loop
thanks for any help
Can you post the full hmtl? It will help us idenitify the problem.
ASKER
<html>
<head>
<script language="javascript">
function testchecked()
{
alert ('in fucntion');
for (var i = 0; i < document.forms['radio'].ra diotest.le ngth; i++) {
alert ('in for loop');
if (document.forms['radio'].r adiotest[i ].checked) {
alert ('in if');
}
}
}
</script>
</head>
<form name="find" action="" method="post">
<td align="left">
<input name="Submit" type="button" class="button" value="click" onClick="javascript:testch ecked()">
</td>
</form>
<table width="100%" border="0" cellpadding="1" cellspacing="0">
<form name="radio">
<tr>
<td width="20%">aaa</td>
<td width="15%">bbb</td>
<td width="20%">ccc</td>
<td width="40%" align="left" ><input type="radio" name="radiotest" value="1234"></td>
</tr>
</form>
</table>
</html>
<head>
<script language="javascript">
function testchecked()
{
alert ('in fucntion');
for (var i = 0; i < document.forms['radio'].ra
alert ('in for loop');
if (document.forms['radio'].r
alert ('in if');
}
}
}
</script>
</head>
<form name="find" action="" method="post">
<td align="left">
<input name="Submit" type="button" class="button" value="click" onClick="javascript:testch
</td>
</form>
<table width="100%" border="0" cellpadding="1" cellspacing="0">
<form name="radio">
<tr>
<td width="20%">aaa</td>
<td width="15%">bbb</td>
<td width="20%">ccc</td>
<td width="40%" align="left" ><input type="radio" name="radiotest" value="1234"></td>
</tr>
</form>
</table>
</html>
ASKER
if i do alert( document.forms['radio'].ra diotest.le ngth);
it comes up with undefined, even though there is a radiotest
it comes up with undefined, even though there is a radiotest
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
btw, when you only have one matching value, the result of the reference is not an array. So with only one "radiotest" input field, this
document.forms[ 1 ].radiotest
references the one and only matching item in the document
document.forms[ 1 ].radiotest
references the one and only matching item in the document
I've been looking for a solution to this problem of mine and was able to attain a resolution with the help of the "Accepted Solution" in this thread.
Thanks 'HonorGod', your solution helped me!
Thanks 'HonorGod', your solution helped me!
SasDev: Good. I'm glad to hear it.