473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Radio Button Question with Javascript


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
2 1149
Fixed it.....I put in a hidden button, just to force it to always be
2........

Jan 26 '06 #2
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: DettCom | last post by:
Hello, I would like to be able to display or hide fields based on whether a specific Yes/No radio button is selected. This is in conjunction with a posting a just made here in the same group...
6
by: HD | last post by:
Hi. I have an asp page with radio buttons and a combobox... when the user clicks a radio button, I want the form to submit so I can execute the ASP code in order to change the list shown in the...
1
by: MickG | last post by:
I am trying to change the value of the variable "hard" according to which radio button is pressed and I am having no joy. Could anyone help me with this, the problematic section is marked with...
5
by: Digital Puer | last post by:
I have the following HTML form: - radio button A (default selected) - radio button B - input field, of type "file" with "Choose" button - submit button I would like to have it so that if the...
3
by: Amelyan | last post by:
When we want radio button to belong to a group name we say, radio1.GroupName="GroupA". In this case, radio1 will be unselected if another radio button is selected in "GroupA". Is there a way...
1
by: kenny8787 | last post by:
Hi, can anyone help here? I have the following code generated from a database, I want to have javascript calculate the costs of the selected items using radio buttons, subtotal the costs and...
9
by: IchBin | last post by:
I can not see what the problem is with this script. I am just trying to set a radio button by calling setCheckedValue('abbr_letter', 'V'). Sorry I am new to javascript. <html> <head> <script...
10
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio...
3
by: jahphill | last post by:
Hey. I thought id make a seperate discusion because the other became a bit lengthy and confusing. Aim: Create a php file which works with the script below, which makes the radiobuttons work =D...
7
by: moksha | last post by:
Hi, I am new to javascript and i am facing a problem in coding. plz help me out. I am using javascript for dynamically creating a table row which contains text boxes and radio...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.