Javascript Bug?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marshall Dudley

    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.leng th)
    for (var i=0;i<document. CC.Ecom_Payment _Card_Type.leng th;i++) {
    if (document.CC.Ec om_Payment_Card _Type[i].checked) {
    var CardType =
    document.CC.Eco m_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

    #2
    Re: Javascript Bug?

    Marshall Dudley said:[color=blue]
    >
    >I am wanting to deteremine which radio button is selected on a form and
    >am using:
    >
    > //alert(document. CC.Ecom_Payment _Card_Type.leng th)
    > for (var i=0;i<document. CC.Ecom_Payment _Card_Type.leng th;i++) {
    > if (document.CC.Ec om_Payment_Card _Type[i].checked) {
    > var CardType =
    >document.CC.Ec om_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?[/color]

    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.

    Comment

    • Richard Cornford

      #3
      Re: Javascript Bug?

      Marshall Dudley wrote:
      <snip>[color=blue]
      > 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?[/color]

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

      Richard.


      Comment

      • RobG

        #4
        Re: Javascript Bug?

        Lee wrote:[color=blue]
        > Marshall Dudley said:[/color]
        [...]
        [color=blue][color=green]
        >>
        >>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?[/color]
        >
        >
        > 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.
        >[/color]

        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#radi o>

        [...]

        --
        Rob

        Comment

        • Marshall Dudley

          #5
          Re: Javascript Bug?

          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.ele ments["Ecom_Payment_C ard_Type"];
          if(typeof radioCollection != "number" ){
          alert('got to one');
          radioCollection = [radioCollection];
          }
          alert(radioColl ection.length);
          for (var i=0;i<radioColl ection.length;i ++) {
          if (radioCollectio n[i].checked) {
          var CardType = radioCollection[i].value
          }
          }
          alert(CardType)

          But if it is more than one button, then alert(radioColl ection.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:
          [color=blue]
          > Marshall Dudley wrote:
          > <snip>[color=green]
          > > 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?[/color]
          >
          > <URL: http://www.jibbering.com/faq/faq_notes/form_access.html >
          >
          > Richard.[/color]

          Comment

          • Marshall Dudley

            #6
            Re: Javascript Bug?

            RobG wrote:
            [color=blue]
            > Lee wrote:[color=green]
            > > Marshall Dudley said:[/color]
            > [...]
            >[color=green][color=darkred]
            > >>
            > >>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?[/color]
            > >
            > >
            > > 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.
            > >[/color]
            >
            > 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.[/color]

            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.
            [color=blue]
            > One radio button should always be selected (but browsers don't enforce it).[/color]

            Yes it will be preselecrted.
            [color=blue]
            > 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.[/color]

            Don't want to allow it to be deselected if there are no other choices.
            [color=blue]
            > 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).
            >[/color]

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

            Marshall
            [color=blue]
            >
            > <URL:http://www.w3.org/TR/html4/interact/forms.html#radi o>
            >
            > [...]
            >
            > --
            > Rob[/color]

            Comment

            • Richard Cornford

              #7
              Re: Javascript Bug?

              Marshall Dudley wrote:
              <snip>[color=blue]
              > if(typeof radioCollection != "number" ){[/color]
              <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.
              [color=blue]
              > Richard Cornford wrote:[/color]
              <snip>

              Top Posting? You are on your own.

              Richard.


              Comment

              • RobG

                #8
                Re: Javascript Bug?

                Marshall Dudley wrote:[color=blue]
                > 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.ele ments["Ecom_Payment_C ard_Type"];
                > if(typeof radioCollection != "number" ){[/color]

                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

                Comment

                • Marshall Dudley

                  #9
                  Re: Javascript Bug?

                  RobG wrote:
                  [color=blue]
                  > Marshall Dudley wrote:[color=green]
                  > > 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.ele ments["Ecom_Payment_C ard_Type"];
                  > > if(typeof radioCollection != "number" ){[/color]
                  >
                  > 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.
                  >[/color]

                  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

                  Comment

                  • Robert

                    #10
                    Re: Javascript Bug?

                    RobG wrote:[color=blue]
                    >
                    > 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.[/color]

                    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.

                    Comment

                    • Lee

                      #11
                      Re: Javascript Bug?

                      Marshall Dudley said:[color=blue]
                      >
                      >RobG wrote:[/color]
                      [color=blue][color=green]
                      >> 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.[/color]
                      >
                      >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.[/color]

                      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.

                      Comment

                      Working...