title: jQuery calculate parent width from children
body: <p>I'm trying to calculate the total width of a submenu relative to the children.
  The following code only works fine in the first submenu, The second submenu needs a manual +1 in the width to make it work, else the last item will be pushed down.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$('nav.primary').find('ul.sub-menu').each( function() {
      var width = 0;
  
      $(this).find('li').each( function() {
          width += $(this).outerWidth();
      });
  
      $(this).css({
          'width': width + 'px'
      });
  
      $(this).hide();
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;li&gt;
      &lt;a href="/oplossingen/"&gt;
         Oplossingen&lt;span class="fo icons-angle-down"&gt;&lt;/span&gt;
      &lt;/a&gt;
        
    &lt;ul class="sub-menu"&gt;
   &lt;li&gt;
        &lt;a href="/oplossingen/mvo-adviesdiensten/"&gt;
          MVO Adviesdiensten
        &lt;/a&gt;
      &lt;/li&gt;      
      &lt;li&gt;
        &lt;a href="/oplossingen/co2-prestatieladder-advies/"&gt;
          CO2-Prestatieladder Advies
        &lt;/a&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>The correct example:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/Hfp8p.png" alt="enter image description here"></p>
  
  <p><strong>The wrong example:</strong> </p>
  
  <p><img src="https://i.stack.imgur.com/qXJ01.png" alt="enter image description here"></p>
  
  <p><strong>Width +1</strong>
  <img src="https://i.stack.imgur.com/7CwQ4.png" alt="enter image description here"></p>
  
  <p>There isn't any other difference between odd and even items except the background color. </p>
  
  <p>I've found the following similar question which couldn't solve my problem:
  <a href="https://stackoverflow.com/questions/1015669/calculate-total-width-of-children-with-jquery">Calculate total width of Children with jQuery</a></p>