title: Jquery foreach pass variable
body: <p>I have a function defined as follows -</p>
  
  <pre><code>function populate() {
          $('input[id^=User]:visible').each(function () {
              currentVal = this.val;
              if (this.id == 'User.FirstName') {
                  $('input[id*=FirstName]:visible').each(function (currentVal) {
                      if (this.id.indexOf("User") &lt; 0)
                          this.value = currentVal;
                  });
              }
          });
      }
  </code></pre>
  
  <p>Essentially what I am trying to do is for every element starting with <code>User</code> I want to populate loop through another collection of elements and assign them the value from the parent loop.
  The problem is passing <code>currentval</code> into the second <code>foreach</code> - it ends being 0,1,2 for some reason.</p>
  
  <p>It's obvious I am not understanding something very fundamental about jquery but I can't articulate this enough for google to be helpful, I tried.
  Thank you!</p>