By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,996 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,996 IT Pros & Developers. It's quick & easy.

Radio Button Question with Javascript

P: n/a

Hi All,

I am using this code to dynamically check is a radio button was checked
on the page. This worked great, if there is more then 1 button in the
group, but fails if there is only 1 button in the group......

Can anyone help debug? I'm stumped.....

function valButton(btn) {;
var cnt = -1;;
for (var i=btn.length-1; i > -1; i--) {;
if (btn[i].checked) {cnt = i; i = -1;};
};
if (cnt > -1) {return btn[cnt].value;};
else {return null;};
};
Thanks,

Jan 26 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Fixed it.....I put in a hidden button, just to force it to always be
2........

Jan 26 '06 #2

P: n/a
am****@iwc.net wrote:
Fixed it.....I put in a hidden button, just to force it to always be
2........


And now you have a hidden trap for unwary maintainers of you page. Why
not test to see what btn is and deal with it appropriately?

function valButton(btn)
{
var len = btn.length;
if ('number' == typeof len) {
for (var i=0; i<len; ++i) {
if (btn[i].checked) return btn[i].value;
}
return null;
}
return (btn.checked)? btn.value : null;
}
--
Rob
Jan 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.