views:

147

answers:

2

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;');

}
A: 

Maybe this helps:

http://stackoverflow.com/questions/2972401/width-off-all-submenu-lis-jquery/2972429#2972429

Base on your comment, give this a shot:

var Sum = 0;

$('ul ul').each(function(){    
   $(this).children('li').each(function(i, e){
      Sum += $(e).outerWidth();
   });

   $(this).width(Sum);
});
jAndy
that gives the width of all off the submenu li's. i would like to have the width off the li's that are in the ul.so in submenu 1 is the total width 100px this is added to the ul off submenu 1. submenu 2 has a total of 200px this is added to the ul off submenu 2
Epco
@Epco: Oh just realized you are the same guy :) I updated the answer, does this fit your requirements?
jAndy
first submenu is now correct :) when i check submenu 2 ul he added the total of submenu 1 and 2 together. example when sub 1 li are 100px and submenu 2 li are 50px then ul of submenu 2 now gets 150px width.
Epco
A: 

thnx for the help. I have this now, it's add's the width to its parrent ul.

$('#menu ul li > ul').each(function(){  
        var liItems = $(this);
        var Sum = 0;
        if(liItems.children('li').length >= 1){
         $(this).children('li').each(function(i, e){
                Sum += $(e).outerWidth(true);
         });
        $(this).width(Sum+1);
        } 
});
Epco