<table>
<tr>
<td><input value = "100/100"/></td>
<td><input value = "100/100"/></td>
<td><input value = "100/100"/></td>
<td class="total">300/300</td>
</tr>
<tr>
<td><input value = "200/200"/></td>
<td><input value = "200/200"/></td>
<td><input value = "200/200"/></td>
<td class="total">600/600</td>
</tr>
</table>
$(document).ready(function(){
$("input").keyup(function(){
var suma = 0;
$(this).parents("tr").children("td").children().each(function(){
suma+= parseInt($(this).val());
});
$(this).parent().parent().children(":last").text(suma);
});
});
The above jquery function gets me the sum when I enter numeric values, but I want to change the my input value format using delimiter "/" example 10/20. I want the sum like this example 10/20 + 20/30 = 30/50. I know that my input values will be in the form of String and will have to parse the numerator and denominator values , add them and put it back as total in String format. Please help.
@Priya Srivatsa, try with below solution
$(document).ready(function(){ $("input").keyup(function(){ var suma = 0; var firstSum = 0; var secondsum = 0; $(this).parents("tr").children("td").children().each(function(){ // suma+= parseInt($(this).val()); var getVal = $(this).val(); if(getVal.indexOf("/")>-1) { var sumVals = getVal.split('/'); firstSum+= parseInt(sumVals[0]); secondsum+= parseInt(sumVals[1]); } else{ firstSum+= parseInt(getVal); } }); $(this).parent().parent().children(":last").text(firstSum + "/" +secondsum ); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td><input value = "100/100"/></td> <td><input value = "100/100"/></td> <td><input value = "100/100"/></td> <td class="total">300/300</td> </tr> <tr> <td><input value = "200/200"/></td> <td><input value = "200/200"/></td> <td><input value = "200/200"/></td> <td class="total">600/600</td> </tr> </table>
Let me know is this solution works for you?
You can reduce
over each input
in a tr
and sum up the split
value
s:
$("input").keyup(function() { const tr = this.closest('tr'); const [sumA, sumB] = [...tr.querySelectorAll('input')] .reduce(([sumA, sumB], input) => { const [inputA, inputB] = input.value.split('/').map(Number); return [sumA + inputA, sumB + inputB]; }, [0, 0]); tr.querySelector('.total').textContent = sumA + '/' + sumB; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td><input value="100/100" /></td> <td><input value="100/100" /></td> <td><input value="100/100" /></td> <td class="total">300/300</td> </tr> <tr> <td><input value="200/200" /></td> <td><input value="200/200" /></td> <td><input value="200/200" /></td> <td class="total">600/600</td> </tr> </table>
You can do it this way : https://codepen.io/creativedev/pen/mKXXWm
$(document).ready(function(){
$("input").keyup(function(){
var suma = 0;
var sumb = 0;
$(this).parents("tr").children("td").children().each(function(){
var enteredval = $(this).val().split("/");
suma+= parseInt(enteredval[0]);
sumb+= parseInt(enteredval[1]);
});
$(this).parent().parent().children(":last").text(suma+"/"+sumb);
});
});
You need to split the text before you change the value.
Javascript has split() method, in your case you can split by "/" which will give you numerator and denominator. Do the sum on both and while displaying it again display by combining both with "/" at mid.
http://jsfiddle.net/gvdfpkh5/9/
<table>
<tr>
<td><input value = "100/100"/></td>
<td><input value = "100/100"/></td>
<td><input value = "100/100"/></td>
<td id="test" class="total">300/300</td>
</tr>
<tr>
<td><input value = "200/200"/></td>
<td><input value = "200/200"/></td>
<td><input value = "200/200"/></td>
<td class="total">600/600</td>
</tr>
</table>
$(document).ready(function(){
$("input").keyup(function(){
var sumd = 0;
var sumn = 0;
$(this).parents("tr").children("td").children().each(function(){
var val = $(this).val().split("/");
sumd+= parseInt(val[0]);
sumn+= parseInt(val[1]);
});
//alert(sumd);
$(this).parents("tr").children("td:last-child").text(sumd+"/"+sumn);
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.