i want to find total width of "li" using jquery. how can i get text width? How can i do it? -
my code below. first alert display "test li 0" , width 1024. 1024 width incorrect. around 50 px. want width of text "test li 0". how can text width?
$(document).ready(function() { var total = 0; var i=0; $('#menutop > span> li').each(function(index) { //var tesi = jquery("#i_"+i).css('width'); var tesi = jquery("#i_"+i).width(); alert(tesi + ':' + $(this).text()); total = parseint(total) + parseint(tesi); i=parseint(i)+1; }); alert(total); }); <ul id="menutop"> <span><li id="i_0" style="background-color:blue;"> test li 0 </li></span> <span><li id="i_1" style="background-color:blue; width:150px;"> test li 1 </li></span> <span><li id="i_2" style="background-color:blue; width:200px;"> test li 2 </li></span> </ul>
as pekka suggested, can put span inside li , use algorithm below:
$(document).ready(function() { var total = 0; var i=0; $('#menutop > li').each(function(index) { //var tesi = jquery("#i_"+i).css('width'); var tesi = $(document.getelementbyid("i_"+i).firstchild).width(); alert(tesi + ':' + $(this).text()); total = parseint(total) + parseint(tesi); i=parseint(i)+1; }); alert(total); }); <ul id="menutop"> <li id="i_0" style="background-color:blue;"><span> test li 0 </span></li> <li id="i_1" style="background-color:blue; width:150px;"><span> test li 1 </span></li> <li id="i_2" style="background-color:blue; width:200px;"><span> test li 2 </span></li> </ul>
Comments
Post a Comment