473,397 Members | 2,099 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,397 software developers and data experts.

Javascript Bug?

I am wanting to deteremine which radio button is selected on a form and
am using:

//alert(document.CC.Ecom_Payment_Card_Type.length)
for (var i=0;i<document.CC.Ecom_Payment_Card_Type.length;i+ +) {
if (document.CC.Ecom_Payment_Card_Type[i].checked) {
var CardType =
document.CC.Ecom_Payment_Card_Type[i].value
}
}
This works fine if there are 2 or more radio buttons, but if there is
only one, it fails. The alert if I uncomment it says that the length is
undefined, when it should be 1. Anyone know what the problem is and how
I can get around it?

Marshall

Sep 29 '05 #1
10 1865
Lee
Marshall Dudley said:

I am wanting to deteremine which radio button is selected on a form and
am using:

//alert(document.CC.Ecom_Payment_Card_Type.length)
for (var i=0;i<document.CC.Ecom_Payment_Card_Type.length;i+ +) {
if (document.CC.Ecom_Payment_Card_Type[i].checked) {
var CardType =
document.CC.Ecom_Payment_Card_Type[i].value
}
}
This works fine if there are 2 or more radio buttons, but if there is
only one, it fails. The alert if I uncomment it says that the length is
undefined, when it should be 1. Anyone know what the problem is and how
I can get around it?


It's got nothing to do with Javascript, it's how the DOM is defined.
If there is only one form control with a given name, it's not modeled
as an array, and so it has no length attribute.

Test for the existence of the length attribute before trying to use it.

Sep 29 '05 #2
Marshall Dudley wrote:
<snip>
This works fine if there are 2 or more radio buttons, but
if there is only one, it fails. The alert if I uncomment
it says that the length is undefined, when it should be 1.
Anyone know what the problem is and how I can get around it?


<URL: http://www.jibbering.com/faq/faq_notes/form_access.html >

Richard.
Sep 29 '05 #3
Lee wrote:
Marshall Dudley said:

[...]

This works fine if there are 2 or more radio buttons, but if there is
only one, it fails. The alert if I uncomment it says that the length is
undefined, when it should be 1. Anyone know what the problem is and how
I can get around it?

It's got nothing to do with Javascript, it's how the DOM is defined.
If there is only one form control with a given name, it's not modeled
as an array, and so it has no length attribute.


Further, radio buttons should never be used singly. If a single
checkable element is indicated, it should be a checkbox. Try to uncheck
a solitary radio button.

One radio button should always be selected (but browsers don't enforce
it). Once a single button is selected, there are no others to check so
you can't uncheck a solo button it without re-setting the form. Some
browser developer may also decide to check the first radio button by
default if the page author hasn't decided which one should be checked by
default (I don't know of any that do so, but the specification could be
interpreted that way).

<URL:http://www.w3.org/TR/html4/interact/forms.html#radio>

[...]

--
Rob
Sep 29 '05 #4
I followed those instructions, and now if there is only one button, it
works as it should, but if there is more than one button, that stopped
working. This is what I did:

var radioCollection
radioCollection =
document.CC.elements["Ecom_Payment_Card_Type"];
if(typeof radioCollection != "number" ){
alert('got to one');
radioCollection = [radioCollection];
}
alert(radioCollection.length);
for (var i=0;i<radioCollection.length;i++) {
if (radioCollection[i].checked) {
var CardType = radioCollection[i].value
}
}
alert(CardType)

But if it is more than one button, then alert(radioCollection.length)
gives me a one (instead of 2 or more) and alert(CardType) returns
undefined. Also I get the alert of 'got to one', which I didn't think I
should get if there is more than one entry. Before I was getting
undefined if there was one button. Is there any way to make it work
with both one and more than one button?

Thanks,

Marshall

Richard Cornford wrote:
Marshall Dudley wrote:
<snip>
This works fine if there are 2 or more radio buttons, but
if there is only one, it fails. The alert if I uncomment
it says that the length is undefined, when it should be 1.
Anyone know what the problem is and how I can get around it?


<URL: http://www.jibbering.com/faq/faq_notes/form_access.html >

Richard.


Sep 29 '05 #5
RobG wrote:
Lee wrote:
Marshall Dudley said: [...]

This works fine if there are 2 or more radio buttons, but if there is
only one, it fails. The alert if I uncomment it says that the length is
undefined, when it should be 1. Anyone know what the problem is and how
I can get around it?

It's got nothing to do with Javascript, it's how the DOM is defined.
If there is only one form control with a given name, it's not modeled
as an array, and so it has no length attribute.


Further, radio buttons should never be used singly. If a single
checkable element is indicated, it should be a checkbox. Try to uncheck
a solitary radio button.


No, it should never be unchecked, that would make the script fail. We have a
case where the store owner selects which credit cards he wants to allow, Visa,
Mastercard, Discover or Amex. If he only selects one, then only that one
should be allowed, if none were selected the the card will not get processed
properly.
One radio button should always be selected (but browsers don't enforce it).
Yes it will be preselecrted.
Once a single button is selected, there are no others to check so
you can't uncheck a solo button it without re-setting the form.
Don't want to allow it to be deselected if there are no other choices.
Some
browser developer may also decide to check the first radio button by
default if the page author hasn't decided which one should be checked by
default (I don't know of any that do so, but the specification could be
interpreted that way).

The script always selects the first button (even if the only button) as
selected.

Marshall

<URL:http://www.w3.org/TR/html4/interact/forms.html#radio>

[...]

--
Rob


Sep 29 '05 #6
Marshall Dudley wrote:
<snip>
if(typeof radioCollection != "number" ){ <snip>

In programming it is generally a good idea to try to understand what you
are trying to do, and failing that accurate reading is a good idea.
Richard Cornford wrote:

<snip>

Top Posting? You are on your own.

Richard.
Sep 29 '05 #7
Marshall Dudley wrote:
I followed those instructions, and now if there is only one button, it
works as it should, but if there is more than one button, that stopped
working. This is what I did:

var radioCollection
radioCollection =
document.CC.elements["Ecom_Payment_Card_Type"];
if(typeof radioCollection != "number" ){


You missed a bit-------------------^^

if(typeof radioCollection.length != "number"){

If you are using the code from the FAQ, then look at the length property
as a way of telling if you have an HTML collection or an element.

PS. Top posting is frowned up!! :-)

[...]

--
Rob
Sep 29 '05 #8
RobG wrote:
Marshall Dudley wrote:
I followed those instructions, and now if there is only one button, it
works as it should, but if there is more than one button, that stopped
working. This is what I did:

var radioCollection
radioCollection =
document.CC.elements["Ecom_Payment_Card_Type"];
if(typeof radioCollection != "number" ){


You missed a bit-------------------^^

if(typeof radioCollection.length != "number"){

If you are using the code from the FAQ, then look at the length property
as a way of telling if you have an HTML collection or an element.


Bingo! Thanks, I had changed so many things trying to get it to work, I
somehow managed to lose the .length from that conditional. Put it in and
now it is working as desired. (Unfortunately I am a perl programmer that has
little understanding of javascript).

Many Thanks,

Marshall

Sep 30 '05 #9
RobG wrote:

Further, radio buttons should never be used singly. If a single
checkable element is indicated, it should be a checkbox. Try to uncheck
a solitary radio button.


I think if there is only one choice, then there should not be any radio
button (or checkbox) visible for it. The choice is implied automatically.

Robert.
Sep 30 '05 #10
Lee
Marshall Dudley said:

RobG wrote:

Further, radio buttons should never be used singly. If a single
checkable element is indicated, it should be a checkbox. Try to uncheck
a solitary radio button.


No, it should never be unchecked, that would make the script fail. We have a
case where the store owner selects which credit cards he wants to allow, Visa,
Mastercard, Discover or Amex. If he only selects one, then only that one
should be allowed, if none were selected the the card will not get processed
properly.


So you're generating the page. That means that you can generate (or at
least modify) the script on the page. If there is only one card type
available, don't bother to check to see what it is on the client side.

Sep 30 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.