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

Javascript Bug?

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.