3 Replies Latest reply on Nov 26, 2014 8:14 AM by MatLac

    Searching for empty field and populate if empty - Adobe/Javascript

    Shadowaleks

      Hi Guys,

       

      Super noob and just getting some code going.

      After coding up something to finally work I have gotten stuck.

       

      Trying to establish a check box system where by if you cross/check something it populates a txt field on next page to allow user to fill in issue. (Hazard log)


      Ideally, would like to know how to;

      - check/cross a box

      - search (set of fields) for empty space

      - if empty, populate field with text.

      - if not empty, continue until empty text field is found.

       

      Sounds complicated to me but I'm probably just being super noob about it

       

      Any help is greatly appreciated !!!

        • 1. Re: Searching for empty field and populate if empty - Adobe/Javascript
          MatLac Level 1

          There's a lot of things in there.

           

          Check/Cross a box is done with the "isBoxChecked(0)" method.  it returns true if box is checked and false otherwise.  You can also check the box with "checkThisBox(0, true)".

           

          To search for empty fields in a doc, you need to loop through all fields of the document one after another, compare their value to an empty string, and put some text if empty.  You will loop throught all fields with the following:

           

          var emptyField = false;  //create a boolean to use later

          var thisField = "";  //Initialize an empty variable to put our fields in;

          for (var i = 0; i < this.numFields; i++){

               thisField = this.getField(this.getNthFieldName(i)) ;      //The field we will be working on

               emptyField = thisField.value == "";                            //If field is empty, boolean is set to true

               if (emptyField) thisField.value = "some text";             //if true, puts text in the empty field

          }

           

          This loop will repeat itself for as long as there are fields in the document.

           

          instead of looking for empty fields by comparing its value to an empty string (""), you can compare its value to its default value which is often an empty field

           

          thisField.value == thisField.defaultValue;

           

          **Note that == is not a typo.  When comparing things, == is the comparison operator while = is an assignation operator (used to put something in a variable)

          • 2. Re: Searching for empty field and populate if empty - Adobe/Javascript
            gkaiseril MVP & Adobe Community Professional

            Text fields have null string value when empty, unless there is default value set for the field.

             

            If a check box is within an exclusionary group of check boxes the value will be "Off" if none of the check boxes have been selected. The same applies to radio buttons.


            • 3. Re: Searching for empty field and populate if empty - Adobe/Javascript
              MatLac Level 1

              If you only need to loop throught SOME of the fields, I would suggest to mark those as "required" and then, in you script, add a condition to look for the .required property to be true.

               

              emptyAndRequiredField = (thisField.value == "") && (thisField.required == true);

              if (emptyAndRequiredField) thisField.value = "some text";