How to iterate through html elements and get combined time with javascript/jQuery? -
How to iterate through html elements and get combined time with javascript/jQuery? -
here jsfiddle.
i'm trying iterate through multiple plans, , calculate combined clip lengths minutes:seconds per plan. i'm missing obvious here probably...
here html:
<div id="right" class="active"> <div class="plan"> <span id="pl-title" contenteditable="true">plan 1</span> <ul id="pl" class="sort"> <li class="note yellow"> <span id="note-title">first clip</span> <span id="note-time">4:00</span> </li> <li class="note pink"> <span id="note-title">second clip</span> <span id="note-time">0:45</span> </li> </ul> <span id="pl-length">length: <span id="pl-time">0:00</span></span> </div> <div class="plan"> <span id="pl-title" contenteditable="true">plan 2</span> <ul id="pl" class="sort"> <li class="note yellow"> <span id="note-title">third clip</span> <span id="note-time">3:30</span> </li> <li class="note pink"> <span id="note-title">fourth clip</span> <span id="note-time">1:18</span> </li> </ul> <span id="pl-length">length: <span id="pl-time">0:00</span></span> </div> </div>
here's javascript (i'm using jquery):
function calctime () { $('.plan').each(function(){ var min = []; var sec = []; $('#pl li #note-time').each(function(){ var info = $(this).text(); var time = data.split(':'); min.push(time[0]); sec.push(time[1]); }); var totalmin = 0; var totalsec = 0; for(var = 0; < min.length; i++){ var thisval = parseint(min[i]); if(!isnan(thisval)){ totalmin += thisval; } } for(var = 0; < sec.length; i++){ var thisval = parseint(sec[i]); if(!isnan(thisval)){ totalsec += thisval; } } // convert seconds var m = (totalmin * 60); var s = (totalsec + m); // convert min:sec minv = math.floor(s / 60); secv = s % 60; if (secv == 0){ var secv = "00"; } $('#pl-time').text(minv+':'+secv); }); }
thanks help!
http://jsfiddle.net/shths/6/
in fiddle, i'm referencing current '.plan' div allows value calculated properly.
changing:
$('#pl li #note-time').each(function(){
to:
$(this).find('#pl li #note-time').each(function(){
javascript jquery math loops
Comments
Post a Comment