We can add the values of two columns and show the result in another column using jQuery easily.
Html code:
<div id="main">
<table>
<tbody>
<tr>
<th>Digit1</th>
<th>Operator</th>
<th>Digit2</th>
<th>Equals</th>
<th>Total</th>
</tr>
<tr>
<td>52</td>
<td>+</td>
<td>42</td>
<td>=</td>
<td></td>
</tr>
<tr>
<td>21</td>
<td>+</td>
<td>57</td>
<td>=</td>
<td></td>
</tr>
<tr>
<td>111</td>
<td>+</td>
<td>45</td>
<td>=</td>
<td></td>
</tr>
</tbody>
</table>
</div>
jQuery code:
$('#main tr').each(function(i){
if(i==0)return true;
var tr = $(this);
var num1 = parseInt($('td', tr).eq(0).text());
var num2 = parseInt($('td', tr).eq(2).text());
$('td', tr).eq(4).text(num1 + num2);
});
In the above example we have taken first column value(0th index) in num1 variable and second column value whose index is 2 in num2 variable.
parseInt method has been used to convert the string data into integer so that we can perform arithmetic operations on the values.
Sum of two numbers will be appear on last column with index 4.
You can see the output here : https://jsfiddle.net/3f19qsuf/2/
0 Comment(s)