add width li's to parent ul

Posted by Epco on Stack Overflow See other posts from Stack Overflow or by Epco
Published on 2010-06-09T08:04:30Z Indexed on 2010/06/09 8:12 UTC
Read the original article Hit count: 360

Filed under:
|
|
|
|

i would like to calculate the width of the li's for each submenu en then put in the ul element.

example

<ul>
 <li>level 1</li>
 <li>
  <ul style="widht:??;"> // total width li's added here 70px
   <li>level 2</li> // width of this li 20px
   <li>level 2</li>/ width of this li 50px
  </ul>
 </li>
 <li>level 1</li>
<li>level 1
  <ul style="widht:??;"> // total width li's added here 70px
   <li>level 2</li> // width of this li 20px
   <li>level 2</li>/ width of this li 50px
  </ul>
</li>
</ul>

i have this so fare

if ('#menu li:has(ul) ul'){

   $('li').each(function(){

        widthParentUl += $(this).outerWidth(true)
    });

$(this)parent.('ul').attr('style','width:' + widthParentUl +'px; display:block; background:red;');

}

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about width