I have the following counter:
- It is inside a table in which foods are selected through a checkbox; So, its function is to calculate the sum of the kcal of the foods that are selected.
-
The problem is that it is used in several sections of the same page and it started with the addition of the previous checkbox. Therefore, I tried to improve it with the properties of the javascript objects, hence caloriasT ['name']. The problem is that it returns 'NaN'. Where this error? Thank you!
caloriasT={}; function contador(checkbox,kcal,nombre) { var sum=parseFloat(kcal); if($(checkbox).is(":checked")){ caloriasT['nombre']+=sum; }else{ caloriasT['nombre']-=sum; } $("#"+nombre).html(Math.abs(caloriasT['nombre']).toFixed(2)+' Kcals'); }