Reputation: 230
What I want to do is subtract from list on numbers just one number. For example I have in a table following numbers and I want to subtract 4000:
<table>
<thead>
<tr>
<td>result</td>
<td>start</td>
<tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>3000</td>
</tr>
<tr>
<td>0</td>
<td>3500</td>
</tr>
<tr>
<td>0</td>
<td>4000</td>
</tr>
</tbody>
</table>
A perfect result would look like this:
<table>
<thead>
<tr>
<td>result</td>
<td>start</td>
<tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>3000</td>
</tr>
<tr>
<td>2500</td>
<td>3500</td>
</tr>
<tr>
<td>4000</td>
<td>4000</td>
</tr>
</tbody>
</table>
Here's a link what I've tried already:
http://jsfiddle.net/tiitremmel/LBjVJ/9/
Upvotes: 2
Views: 570
Reputation: 227270
Does this do what you want? jsFiddle
EDIT: New Version!
var subtract = 4000;
$('#tbl tbody tr').each(function(){
var value = parseInt($(this).find('td:eq(1)').text());
var $result = $(this).find('td:eq(0)');
if(subtract >= value){
$result.text(0);
subtract -= value;
}
else if(subtract <= 0){
$result.text(value);
}
else{
$result.text(value-subtract);
subtract -= value;
}
});
Upvotes: 1
Reputation: 20602
If you want to remove a row that has the first cell as your target number then try:
var targetNumber = 4000;
// Note, better to use the ID of the table instead of 'table' here
$('table tr').each(function() {
var tCell = $(this).find('td:first');
if (parseInt(tCell.text()) == targetNumber) {
$(this).remove();
}
});
Upvotes: 0