Reputation: 109
My script should counts values in table but it can't replace '%' char and shows 'undefined'. I tried some solution but no luck.
I made a fiddle: fiddle
$('table .ilosc, table .cena_netto .stawka_vat').on('keyup paste change', function() {
$('table tr').each(function() {
var ilosc = $(this).find('input.ilosc').val();
var cena_netto = $(this).find('input.cena_netto').val();
var wartosc_netto = (ilosc * cena_netto);
$(this).find('input.wartosc_netto').val(wartosc_netto);
var stawka_vat = $(this).find('input.stawka_vat').val().replace('%', '');
var kwota_vat = ( wartosc_netto * (stawka_vat / 100) );
$(this).find('input.kwota_vat').val(kwota_vat);
var wartosc_brutto = (wartosc_netto + kwota_vat);
$(this).find('input.wartosc_brutto').val(wartosc_brutto);
}); //END .each
return false;
}); // END change
It works fine until line: $(this).find('input.wartosc_netto').val(wartosc_netto);
Upvotes: 1
Views: 204
Reputation: 2783
Alvin Bakker was close, but it needed a bit more tuning.
The problem is that you run over each row, including the header row. The header row doesn't contain the values you want, so it runs into an error, and javascript stops execution on errors.
A simple way to solve this, is by checking if you're in the header row:
$('table .ilosc, table .cena_netto .stawka_vat').on('keyup paste change', function() {
$('table tr').each(function() {
//NEW CODE HERE:
//Pseudo code: if this row contains <th>-elements,
//then don't bother with it and move on to the next row.
if ($(this).find('th').length > 0)
return;
var ilosc = $(this).find('input.ilosc').val();
var cena_netto = $(this).find('input.cena_netto').val();
var wartosc_netto = (ilosc * cena_netto);
$(this).find('input.wartosc_netto').val(wartosc_netto);
var stawka_vat = $(this).find('input.stawka_vat').val().replace('%', '');
var kwota_vat = ( wartosc_netto * (stawka_vat / 100) );
$(this).find('input.kwota_vat').val(kwota_vat);
var wartosc_brutto = (wartosc_netto + kwota_vat);
$(this).find('input.wartosc_brutto').val(wartosc_brutto);
}); //END .each
return false;
}); // END change
Alternative solution (inspired by comment of @SlashmanX)
You could also adapt your html a bit:
Put your first row in a <thead>
element. Like this:
<table>
<thead>
<tr>
<th>Lp.</th>
<th>Nazwa towaru lub usługi</th>
<th>Jednostka</th>
<th>Ilość</th>
<th>Cena netto</th>
<th>Wartość netto</th>
<th>Stawka VAT</th>
<th>Kwota VAT</th>
<th>Wartość brutto</th>
</tr>
</thead>
<!-- All data rows below -->
</table>
Then you can adapt your javascript like this:
$('table .ilosc, table .cena_netto .stawka_vat').on('keyup paste change', function() {
//Subtle difference in this line: Spot the '>' in the selector!
$('table>tr').each(function() {
var ilosc = $(this).find('input.ilosc').val();
var cena_netto = $(this).find('input.cena_netto').val();
var wartosc_netto = (ilosc * cena_netto);
$(this).find('input.wartosc_netto').val(wartosc_netto);
var stawka_vat = $(this).find('input.stawka_vat').val().replace('%', '');
var kwota_vat = ( wartosc_netto * (stawka_vat / 100) );
$(this).find('input.kwota_vat').val(kwota_vat);
var wartosc_brutto = (wartosc_netto + kwota_vat);
$(this).find('input.wartosc_brutto').val(wartosc_brutto);
}); //END .each
return false;
}); // END change
Again: credit to SlashmanX for pointing this out.
Upvotes: 1
Reputation: 1526
I think I found it... You do an each on all <tr>
rows. The first row is a header. So it will not find any values. Replace that row with <thead>
and then the undefined
error doesnt occur. When an error like this happens, the rest of the script will stop.
Even better is to give the <tr>
a class and then loop through the classes.
Mind you, make a fall back, when no value is found BEFORE you do the replace
Upvotes: 1