5 Replies Latest reply on Feb 4, 2017 1:14 AM by try67

    iterate through and push SPECIFIC checkboxes to array

    coldcast

      Hello,

       

      I have a document with a lot of different checkboxes and they all have their own specific category.

       

      I have the document so that when a user clicks on a checkbox under that specific category, a button displays a value of the number of checkmarks that are clicked under that specific category.  The program will only display a value, if the checkbox string value is in the array which I DEFINE.


      I have had to manually type in the specific checkbox name into array to get this done, but I am looking to see if I can iterate through and select a specific set of checkboxes  to then PUSH to an array, so I don't have to manually type the string names of 100+ checkboxes.

       

      Here is the code that I put in my custom calculation script of the text field which displays the number of checkboxes typed in:

       

      var checkboxes = ["holter-48hr", "holter-72hr", "holter-1wk", "holter-2wk", "cardio-ecgharmonic", "cardio-loop", "cardio-restingecg", "cardio-gxt" ]; 
      
      var counter = 0;  
      for (var i in checkboxes) {  
          if (this.getField(checkboxes[i]).value!="Off") counter++;  
      }  
      if (counter < 1 ) event.value = "";
      else event.value = counter + "C";  
      
      

       

      so I have only 2 questions:

      1) Is it possible to iterate through a bunch of string values and select them based on the presence of a certain word?
      2) Can I toggle the text field to hide (the one above which the script applies to), if the counter value is zero?

       

      Thank you!

        • 1. Re: iterate through and push SPECIFIC checkboxes to array
          try67 MVP & Adobe Community Professional

          1) Do you mean automatically process all the fields that contain a certain word in their names?

          2) It's possible, sure, but why is hiding the field better then just resetting it?

          • 2. Re: iterate through and push SPECIFIC checkboxes to array
            coldcast Level 1

            1) yes,  I would like the loop to look for specific words at the beginning of the string and if a match is found,  to push that string into an array

             

            2) resetting is one option that I don't mind but I was curious about hiding the field because I want the field to appear only when it is relevant for the user.  But having the field remain blank is still an option, if hiding it proves to be too much of an issue or slows the form down.

            • 3. Re: iterate through and push SPECIFIC checkboxes to array
              try67 MVP & Adobe Community Professional

              1) This code will do that for you:

               

              var fields = [];
              for (var i=0; i<this.numFields; i++) {
                  var f = this.getField(this.getNthFieldName(i));
                  if (f==null) continue;
                  if (f.name.indexOf("some string")==0) {
                      fields.push(f.name);
                  }
              }
              

               

              2) You can use this code for that:

              event.target.display = display.hidden;
              

              (replace 'hidden' with 'visible' to revert it back)

              1 person found this helpful
              • 4. Re: iterate through and push SPECIFIC checkboxes to array
                coldcast Level 1

                Hey,

                 

                So I tried what you've put in for the code and now my code looks like this:

                 

                var fields = [];  
                for (var i=0; i<this.numFields; i++) {  
                    var f = this.getField(this.getNthFieldName(i));  
                    if (f==null) continue;  
                    if (f.name.indexOf("ultra") == 0) {  
                        fields.push(f.name);  
                    }  
                }  
                var counter = 0;  
                for (var i in fields) {  
                    if (this.getField(fields[i]).value!="Off") counter++;  
                }
                if (counter < 1 ) event.target.display = display.hidden;
                else event.target.display = display.visible;    
                if (counter < 1 ) event.value = "";  
                else event.value = counter + "U";  
                


                The problem is that when I put this code in, the field displays " 1 U" even though I haven't checked anything off in that specific category. I do have ONE text field which is named "ultra-other" and is NOT a checkbox. Could this be affecting the loop?

                 

                The checkboxes I plan to target are strings which include the word ultra in them like "ultra-chest" or "ultra-thyroid" etc etc.

                 

                Not sure where to go from here :S

                • 5. Re: iterate through and push SPECIFIC checkboxes to array
                  try67 MVP & Adobe Community Professional

                  Yes, that will be it... To solve it, change this line:

                  if (f.name.indexOf("ultra") == 0) {

                  To:

                  if (f.type=="checkbox" && f.name.indexOf("ultra") == 0) {