title: Javascript array not working as expected
body: <p>I'm pretty new to js/jquery. For each checkbox with the ID of check$ (where $ is a sequential number), I want to toggle the class "agree" of the surrounding span that uses the same check$ (but as a class). I don't want to have to hard-code the list of matching checkboxes, as this may vary.</p>
  
  <p>Here's my code. This function works as expected:</p>
  
  <pre><code>agree = function (checkbox, span) {
      $(checkbox).change(function(){
         $(span).toggleClass('agree');
     });      
  };
  </code></pre>
  
  <p>This is what I'm trying to pass to the above function, which does not work:</p>
  
  <pre><code>$(function() {
      var elemid = 'check',
      checks     = Array($('[id^='+elemid+']').length);
      console.log(checks);
    for (i=0; i &lt; checks; i++) {
      agree('#'+elemid+checks[i], "."+elemid+checks[i]);
      }
  });
  </code></pre>
  
  <p>console.log(checks) returns <strong>[undefined × 4]</strong>. The number of elements is correct, but I don't know why it's undefined, or whether that is even significant.</p>
  
  <p>The following code works as expected, but as I say, I'd rather not have to specify every matched element:</p>
  
  <pre><code>$(function() {
      var checks = ["check1", "check2", "check3", "check4"];
      for (i=0; i &lt; checks.length; i++) {
          agree('#'+checks[i], "."+checks[i]);
          }
  });
  </code></pre>
  
  <p>Thanks.</p>
  
  <p>Edit: Thanks to <a href="https://stackoverflow.com/users/680420/jack">Jack</a>, I was overlooking the most simple method. I added the same class to all checkboxes and spans, and solved the problem with this:</p>
  
  <pre><code>$('input.check').change(function(){
      $(this).closest('span.check').toggleClass('agree');
  });
  </code></pre>