title: jQuery "for" loop
body: <p>I am trying to write some very basic jQuery code here.</p>
  
  <p>What I am doing here is I want the image to toggle when hover. I have 5 images, and I was able to archive that when I wrote 5 codes, 1 for each image.</p>
  
  <pre><code>        $('#menu &gt; ul:first-child &gt; li:nth-child(1)').hover(function(){
              $('li:nth-child(1) &gt; .image-hover').toggle();
              $('li:nth-child(1) &gt; .image').toggle();
          });
  
          $('#menu &gt; ul:first-child &gt; li:nth-child(2)').hover(function(){
              $('li:nth-child(2) &gt; .image-hover').toggle();
              $('li:nth-child(2) &gt; .image').toggle();
          });
  
          $('#menu &gt; ul:first-child &gt; li:nth-child(3)').hover(function(){
              $('li:nth-child(3) &gt; .image-hover').toggle();
              $('li:nth-child(3) &gt; .image').toggle();
          });
  
          $('#menu &gt; ul:first-child &gt; li:nth-child(4)').hover(function(){
              $('li:nth-child(4) &gt; .image-hover').toggle();
              $('li:nth-child(4) &gt; .image').toggle();
          });
  
          $('#menu &gt; ul:first-child &gt; li:nth-child(5)').hover(function(){
              $('li:nth-child(5) &gt; .image-hover').toggle();
              $('li:nth-child(5) &gt; .image').toggle();
          });
  </code></pre>
  
  <p>Now I want to shorten the code, using the "for" loop. The idea is I have a variable i, starting from 1, increasing 1 by 1 up to 5. The nth-child above, instead of having a specific number, will now hold i. As i changes, nth-child(i) will have respective values of i.</p>
  
  <pre><code>        (function($){
              for (var i = 1; i &lt; 6; i++) {
                  $('#menu &gt; ul:first-child &gt; li:nth-child(i)').hover(function(){
                      $('li:nth-child(i) &gt; .image-hover').toggle();
                      $('li:nth-child(i) &gt; .image').toggle();
                  });
              };
          })(jQuery);
  </code></pre>
  
  <p>This shorten code doesn't work though.</p>
  
  <p>Can someone help me with this? Thanks a lot.</p>