title: While loop in jquery of dynamic id and class
body: <p>I have have multiple divs' with similar code within it, but also has a unique id within the main div that calls a toggleClass &amp; slideToggle function. I'm trying to create a loop so that I do not have to write similar code for each element.</p>
  
  <p>--- working code --- (where numeric value after the id would change)</p>
  
  <pre><code>$('#er1').click(function() {
      $('#er1').toggleClass('but-extra-on');
      $('#cr1').toggleClass('but-closerow-on');
      $('#er1').next('div').slideToggle('slow');
      return false;
  });
  </code></pre>
  
  <p>-- not working code -- (I want to have functions for the click of #er1, #er2, #er3 etc.)</p>
  
  <pre><code>var count = 1;
  while (count &lt; 10){
      var curER = 'er'+count; 
      var curCR = 'cr'+count; 
      $('#'+curER).click(function() {
          $('#'+curER).toggleClass('but-extra-on');
          $('#'+curCR).toggleClass('but-closerow-on');
          $('#'+curER).next('div').slideToggle('slow');
      });
      count++;
  }
  </code></pre>
  
  <p><em>*</em> for some reason, when I use the while code, whenever I click #er1, #er2, #er3 etc.. only the event for #er9 toggles.</p>