I have this code to make some calculation when I change the inputs fields:
<div class="card-body">
<div class="form-group">
<label>Pret vanzare</label>
<input id="price_vanzare" type="text" name="price" class="form-control" placeholder="Product price" value="<?php echo $product->price; ?>">
</div>
<div class="form-group">
<label>Pret achizitie</label>
<input id="price_achizitie" type="text" name="price_achizitie" class="form-control" placeholder="Product price" value="<?php echo $product->price_achizitie; ?>">
</div>
<div class="form-group">
<label>Pret transport</label>
<input id="price_transport" type="text" name="price_transport" class="form-control" placeholder="Product price" value="<?php echo $product->price_transport; ?>">
</div>
<div class="form-group">
<label>Profit net</label>
<input id="profit" type="text" class="form-control">
</div>
</div>
and jQuery code:
$(":input[type='text']").keyup(function(){
var pret_achizitie = $('#price_achizitie').val();
var pret_vanzare = $('#price_vanzare').val();
var pret_transport = $('#price_transport').val();
var profit_net = ((pret_vanzare - ((pret_achizitie*1.19) + pret_transport + (0.19 * pret_transport))) - ((pret_vanzare * 0.19/1.19) - ((pret_achizitie * 0.19)+(pret_transport * 0.19)))) - (1/100) * (pret_vanzare -(pret_vanzare * 0.19/1.19));
$('#profit').val(parseFloat(profit_net).toFixed(2));
} );
Before keyup I have:
var pret_achizitie = <?php echo $product->price_achizitie; ?>;
var pret_vanzare = <?php echo $product->price; ?>;
var pret_transport = <?php echo $product->price_transport; ?>;
var transport_client = <?php echo $product->transport_client; ?>;
var profit_net = ((pret_vanzare - ((pret_achizitie*1.19) + pret_transport + (0.19 * pret_transport))) - ((pret_vanzare * 0.19/1.19) - ((pret_achizitie * 0.19)+(pret_transport * 0.19)))) - (1/100) * (pret_vanzare -(pret_vanzare * 0.19/1.19));
$('#profit').val(parseFloat(profit_net).toFixed(2));
and everything is working fine but when I change something is returned NaN for #profit input ID
If I change on var profit_net
and put only pret_vanzare - pret_achizitie
it working even I change inputs for pret_vanzare or pret_achizitie
I don’t know what is wrong in var profit_net
because there is something wrong!
2
Answers
make sure all the values are valid number this (pret_achizitie, pret_vanzare, pret_transport, transport_client).