jquery - Summing up values in custom attr loop in JavaScript -


i have loop generates <li> custom attribute filesize, want search <li> class , sum value, instead returns 2 values next each other , not summing values.

https://jsfiddle.net/y6yll0yv/1/

var myelements = jquery(".fileress"); var sumoffilessize = 0; (var = 0; < myelements.length; i++) {     var valueofthis = myelements.eq(i).attr("sizefile");     var valuetostr = valueofthis.tostring();     var sumoffilessize = (valuetostr + sumoffilessize); }  alert(sumoffilessize); 

html:

<div id="fileinfo1">     <div style="clear:both;padding:5px;" class="fileress" sizefile="206519" id="fileres_0">         <input size="1" type="file" class="file" name="attach[]" id="attacher" style="display:none" multiple="" value="undefined">206519 </div>     <div style="clear:both;padding:5px;" class="fileress" sizefile="290284" id="fileres_1">         <input size="1" type="file" class="file" name="attach[]" id="attacher" style="display:none" multiple="" value="undefined">290284 </div> </div>  <div id="result"></div> 

try convert string got sizefile attribute number using parseint(numberstring) before doing summation of it,

var myelements = jquery(".fileress"); var sumoffilessize = 0; (var = 0; < myelements.length; i++) {   sumoffilessize += parseint(myelements.eq(i).attr("sizefile") }  alert(sumoffilessize); 

if not convert it, string concatenation happened since + common string concatenation , mathematical addition. string concatenation when meets single string operand.

demo


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -