title: loop through certain elements inside div
body: <p>I'm having html code like this</p>
  
  <pre><code>&lt;div class="myLinks"&gt;
    &lt;div class="menu"&gt;
      &lt;a href="#"&gt;Home&lt;/a&gt;
      &lt;div class="submenu"&gt;
      &lt;a href="#"&gt;item 1&lt;/a&gt;
      &lt;a href="#"&gt;item 2&lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;div class="menu"&gt;
      &lt;a href="#"&gt;Services&lt;/a&gt;
      &lt;div class="submenu"&gt;
        &lt;a href="#"&gt;item 3&lt;/a&gt;
        &lt;a href="#"&gt;item 4&lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;a href="#"&gt;contact&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>So I have 7 links most of them are inside divs and only 1 link not inside another div which is "Contact". Now I'm trying to give every link an id which equals its index according to other links not all elements inside the main div so I do this</p>
  
  <pre><code>var links= $(".myLinks a").length; //7
  for(var x=1; x&lt;links; x++){
      var i = x-1;
         $(".myLinks a:eq("+i+")").attr("id",i);
      });
  }
  </code></pre>
  
  <p>But this does not work because $(".myLinks a:nth-child("+i+")") is not going to count links only but counts all the items inside .myLinks so i need to give every link an id equals its order among other links</p>
  
  <p>example: <a href="http://jsfiddle.net/a67b3emq/" rel="nofollow">http://jsfiddle.net/a67b3emq/</a></p>