title: PDF JavaScript: Function with a loop
body: <p>Can anyone please let me know what is wrong with my code below? I am trying to create function with a loop, for hiding and making visible labels in the PDF document depending on the user selections.</p>
  
  <p>My original code:
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>if ((type == "sugar") &amp;&amp; (countryofgrowth == "United State"))
  {
         getField("sugarlabel").display=display.visible;
         getField("goodsugar").display=display.hidden;
         getField("sugarQualitylevellow").display=display.hidden;
         getField("sugarQualitylevelhigh").display=display.hidden;
         getField("sugarQualitylevelhighmorethan75").display=display.hidden;
         getField("sugarQualitylevelhighlessthan75").display=display.hidden;
         getField("cornquantity").display=display.hidden;
         getField("cornquantitymorethan500").display=display.hidden;
         getField("cornquantitymax").display=display.hidden;
         getField("cornquantitymaxyes").display=display.hidden;
         getField("cornquantitymaxno").display=display.hidden;
  }
  else if ((type == "sugar") &amp;&amp; (countryofgrowth == "United Kingdom"))
  {
         getField("sugarlabel").display=display.hidden;
         getField("sugarquality").display=display.visible;
         getField("sugarQualitylevellow").display=display.hidden;
         getField("sugarQualitylevelhigh").display=display.hidden;
         getField("sugarQualitylevelhighmorethan75").display=display.hidden;
         getField("sugarQualitylevelhighlessthan75").display=display.hidden;   
         getField("cornquantity").display=display.hidden;
         getField("cornquantitymorethan500").display=display.hidden;
         getField("cornquantitymax").display=display.hidden;
         getField("cornquantitymaxyes").display=display.hidden;
         getField("cornquantitymaxno").display=display.hidden;          
   }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Function with a loop:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var setDisplay = function(display){
    var fields = ["sugarlabel", "goodsugar", "sugarQualitylevellow" /*,...*/];
      for(var i=0; i&lt; fields.length; i++){
        if(fields[i] == display) {
            getField(fields[i]).display=display.visible;
          } else {
            getField(fields[i]).display=display.hidden;
          }
      }
  }
  if ((type == "sugar") &amp;&amp; (countryofgrowth == "United State"))
  { 
  setDisplay("sugarlabel")
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I am keep getting an error: <code>'RangeError: Invalid argument value.</code></p>
  
  <p>Any help is much appreciated</p>