1 Reply Latest reply on Sep 3, 2009 11:49 AM by ksumwalt

    Script not doing else portion of if...  else...

    ksumwalt Level 1

      Ihave a form with many text fields.  I have a specific action i would like to happen with 3 of these fields.  I am trying to determine if, after entering text in 1 of these fields, 2 text fields have data, and, if they do, add that data to the end of a third text field.

       

      I am using the following script as a On Blur action in the field work_item1:

       

      var itemnumber = this.getField("work_item1").valueAsString;
      var itemname = this.getField("work1").valueAsString;
      var itemdesc = this.getField("work_description").value;

       

      if ( itemnumber != "" && itemname != "" )


      {
      itemdesc = itemdesc + itemnumber + " - " + itemname;
      this.getField("work_description").setFocus();
      }


      else

      {
      this.getField("work1").setFocus();
      }

      What happens is that if both fields contain text, focus goes to the work_description field where the text should be appended.  So if the condition is apparently evaluated as true as that portion is partially executing.  It is moving focus to the third field, just not putting a value into the third field.  If the condition is false, the else portion does nto seem to execute as focus does not go to the correct place.

       

      I will be placing the same code in the work1 field, just changing the else focus to the work_item1 field.

       

      I am new to JavaScripting, which may be obvious, and I have noone here who knows it to ask.  So what am I missing here?  I have 2 things not happening:

      1)  The work_description field's value is not being set when it should.

      2)  The else condition is not running.

       

      Regards.

      Karl

        • 1. Re: Script not doing else portion of if...  else...
          ksumwalt Level 1

          OK, I figured this out after putting it away for a little while.  In case anyone is wondering, here is the corrected code.  I had to use the variables for all of the work, and define the variable to the object instead of an object's child.

           

          var itemnumber = this.getField("work_item1");
          var itemname = this.getField("work1");
          var itemdesc = this.getField("work_description");

          if ( itemnumber.valueAsString != "" && itemname.valueAsString != "" )
              {
              itemdesc.value = itemdesc.valueAsString + itemnumber.valueAsString + " - " + itemname.valueAsString;
              itemdesc.setFocus();
              }
          else
              {
              itemname.setFocus();
              }