title: how to put an 'if' statement in front of a change function
body: <p>This question is for using javascript in Caspio Cloud's platform, so a few things might look different than normal. But basically, I need to put an 'if' statement that references a variable 'above' a change function that runs some other code and I can't figure it out. </p>
  
  <p>The problem is that in Caspio's form the Virtual9 field will 'change' on the form's load, and so it runs the change js. I need to not run the change js onload and also only if the variable 'item' is null. </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>&lt;script&gt;
  var item = document.getElementsByName("InsertRecordItemID")[0].value;
    
  &lt;!-- right here I need an if (item !== null) to then run the change function but that if it's not null to not run the change function{ --&gt;
    
  $("[name='cbParamVirtual9']").change(function(){
  
  var itid = document.getElementsByName("cbParamVirtual9")[0].value;
  document.getElementsByName("InsertRecordItemID")[0].value = itid;
  
  }
  );
  
  &lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I've tested the variables using alert windows and they are gotten correctly. I just need to make sure the change function doesn't run if the variable 'item' is null. </p>
  
  <p>EDIT: To clarify, when the form loads the ItemID field may or may not be null (based on parameters that pass over). </p>
  
  <p>If it is not null then the Virtual9 field (where the user makes a selection) will be hidden- because the ItemID already had a value and needs to not change. </p>
  
  <p>However, if ItemID is null then Virtual9 will be visible and the user can make a change, thereby updating the ItemID field. The user may update Virtual9 multiple times (looking for the right record) so it needs to keep updating ItemID (even after it is no longer null in this case). </p>
  
  <p>That's why I think the best solution is to disable the code ONLY for onload, if possible. So that the code runs on change of Virtual9 but not onload. </p>