javascript - jQuery: How to get the width sum of all previous elements and put it in data attribute -


how width of each previous elements , sum them (after page load)

put value in each element data attribute

code on jsfiddle http://jsfiddle.net/jsfkj/2/

how make code

<ul>  <li style="width:130px">num 1</li>  <li style="width:150px">num 2</li>  <li style="width:140px">num 3</li>  <li style="width:175px">num 4</li>  <li style="width:100px">num 5</li> </ul> 

to this

<ul>  <li style="width:130px" data-num="0">num 1</li>  <li style="width:150px" data-num="130">num 2</li>  <li style="width:140px" data-num="280">num 3</li>  <li style="width:175px" data-num="420">num 4</li>  <li style="width:100px" data-num="595">num 5</li> </ul> 

this image explain more want
enter image description here
hope i've explained want well

an easy way : http://jsfiddle.net/jsfkj/5/

you save var add width of element in each :

(function(){     var total = 0;     $('li').each(function() {         $(this).attr('data-num', total)         total += $(this).width()     }); })(); 

this method allow reduce number of each 1


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -