3 Replies Latest reply on Feb 24, 2008 6:21 AM by Greg Dove

    CheckBox conditional logic

    GerryIsHere Level 1
      Hi there!

      I use in a form two CheckBoxes cb1 and cb2.
      field name : selected
      data type: Boolean

      I wrote inside a verification function:

      if ((cb1.data = false) && (cb2.data = false)) {
      //do something
      }
      if ((cb1.data = true) && (cb2.data = false)) {
      //do something
      }
      if ((cb1.data = false) && (cb2.data = true)) {
      //do something
      }
      if ((cb1.data = true) && (cb2.data = true)) {
      //do something
      }

      This doesn't work...
      Operation go straight to the last "if" even if, for example, it should stop at the second "if" as cb1 is checked and cb2 is not.
      What's wrong in my script?

      Many thanks in advance for your help!

        • 1. Re: CheckBox conditional logic
          Greg Dove Level 4
          To check whether a checkbox is selected or not you just check the checkbox instance's selected property.

          Also when you're checking something, you need to make sure you use "==" which is for comparisons instead of "=" which is for assigning a value.

          And, because the selected property of a checkbox is a boolean value, you can just use the selected property's value as the 'check', for example:

          and (ie. both):
          if (cb1.selected && cb2.selected) {do something}

          or:
          if (cb1.selected || cb2.selected) {do something}

          first one and not the second one:
          if (cb1.selected && !cb2.selected) {do something}

          Often you would do this type of checking in an event listener function.
          • 2. Re: CheckBox conditional logic
            GerryIsHere Level 1
            YES YES YES.
            I was fundamentally wrong because of my misunderstanding of CB properties.

            While you were typing I solved by myself the problem writing:

            if ((cb1.selected == false) && (cb2.selected == false)) {
            //do something
            }
            if ((cb1.selected == true) && (cb2.selected == false)) {
            //do something
            }
            if ((cb1.selected == false) && (cb2.selected == true)) {
            //do something
            }
            if ((cb1.selected == true) && (cb2.selected == true)) {
            //do something
            }

            This is "heavy" writing.
            I find your solution simple and elegant and I'll use it for sure!
            Thank you very much and a wonderful Sunday!

            Regards
            • 3. Re: CheckBox conditional logic
              Greg Dove Level 4
              you're welcome!