title: How to condense hardcoded jQuery code to smaller generic code?
body: <p>I have a code that works perfectly fine because it is hardcoded but extremely large. I would like to simplify this code to make it general and of course smaller. Does anybody know how to do this? Since it is so large I will shrink it down for this question.</p>
  
  <pre><code>    var theform1 = $('#form1').find('.state option');        
      var stateForm1 = Object.keys(localStorage).filter(key =&gt; key.endsWith('-state1'));
      var stateChoice1 = JSON.parse(localStorage.getItem(stateForm1));
  
      var theform2 = $('#form2').find('.state option');        
      var stateForm2 = Object.keys(localStorage).filter(key =&gt; key.endsWith('-state2'));
      var stateChoice2 = JSON.parse(localStorage.getItem(stateForm2));
  
      var theform3 = $('#form3').find('.state option');        
      var stateForm3 = Object.keys(localStorage).filter(key =&gt; key.endsWith('-state3'));
      var stateChoice3 = JSON.parse(localStorage.getItem(stateForm3));
  
      if (localStorage.getItem(stateForm1)) {            
          $(theform1).empty();
          $(theform1).val(stateChoice1).append(`&lt;option value="${stateChoice1}"&gt;${stateChoice1}&lt;/option&gt;`).trigger('window.load');                    
      }; 
  
      if (localStorage.getItem(stateForm2)) {            
          $(theform2).empty();
          $(theform2).val(stateChoice2).append(`&lt;option value="${stateChoice2}"&gt;${stateChoice2}&lt;/option&gt;`).trigger('window.load');                    
      };
  
      if (localStorage.getItem(stateForm3)) {            
          $(theform3).empty(); 
          $(theform3).val(stateChoice3).append(`&lt;option value="${stateChoice3}"&gt;${stateChoice3}&lt;/option&gt;`).trigger('window.load');
  
      };        
  </code></pre>
  
  <p>The HTML markup is as follows:</p>
  
  <pre><code>    &lt;form id="form1" autocomplete="off"&gt;
     &lt;select id="country1" class="country" name="country"&gt;
      &lt;?php include("countryAjaxData.php"); ?&gt; 
     &lt;/select&gt; 
     &lt;select id="state1" class="state" name="state"&gt;
      &lt;option value="" selected="selected"&gt;Select state&lt;/option&gt;
     &lt;/select&gt; 
     &lt;select id="city1" class="city" name="city"&gt;
      &lt;option value="" selected="selected"&gt;Select city&lt;/option&gt; 
     &lt;/select&gt;
    &lt;/form&gt;
    &lt;form id="form2" autocomplete="off"&gt;
     &lt;select id="country2" class="country" name="country"&gt;
      &lt;?php include("countryAjaxData.php"); ?&gt; 
      &lt;/select&gt; 
     &lt;select id="state2" class="state" name="state"&gt;
      &lt;option value="" selected="selected"&gt;Select state&lt;/option&gt;
     &lt;/select&gt; 
     &lt;select id="city2" class="city" name="city"&gt;
      &lt;option value="" selected="selected"&gt;Select city&lt;/option&gt; 
     &lt;/select&gt;
    &lt;/form&gt;
    &lt;form id="form3" autocomplete="off"&gt;
     &lt;select id="country3" class="country" name="country"&gt;
      &lt;?php include("countryAjaxData.php"); ?&gt; 
     &lt;/select&gt; 
     &lt;select id="state3" class="state" name="state"&gt;
      &lt;option value="" selected="selected"&gt;Select state&lt;/option&gt;
      &lt;/select&gt; 
     &lt;select id="city3" class="city" name="city"&gt;
      &lt;option value="" selected="selected"&gt;Select city&lt;/option&gt; 
     &lt;/select&gt;
    &lt;/form&gt;
  </code></pre>