Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / September 2005



Tip: Looking for answers? Try searching our database.

Javascript Bug?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marshall Dudley - 29 Sep 2005 20:12 GMT
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
Lee - 29 Sep 2005 21:38 GMT
Marshall Dudley said:

>I am wanting to deteremine which radio button is selected on a form and
>am using:
[quoted text clipped - 11 lines]
>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.
RobG - 29 Sep 2005 22:13 GMT
> 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
[quoted text clipped - 4 lines]
> 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>

[...]

Signature

Rob

Marshall Dudley - 29 Sep 2005 23:30 GMT
> > Marshall Dudley said:
> [...]
[quoted text clipped - 11 lines]
> 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
Lee - 30 Sep 2005 14:59 GMT
Marshall Dudley said:

>> Further, radio buttons should never be used singly. If a single
>> checkable element is indicated, it should be a checkbox. Try to uncheck
[quoted text clipped - 5 lines]
>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.
Robert - 30 Sep 2005 08:30 GMT
> 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.
Richard Cornford - 29 Sep 2005 22:00 GMT
<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.
Marshall Dudley - 29 Sep 2005 23:18 GMT
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

> <snip>
> > This works fine if there are 2 or more radio buttons, but
[quoted text clipped - 5 lines]
>
> Richard.
Richard Cornford - 29 Sep 2005 23:53 GMT
<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.

<snip>

Top Posting? You are on your own.

Richard.
RobG - 30 Sep 2005 00:06 GMT
> 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
[quoted text clipped - 4 lines]
> 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!! :-)

[...]

Signature

Rob

Marshall Dudley - 30 Sep 2005 01:27 GMT
> > 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
[quoted text clipped - 11 lines]
> 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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.