Odie Odie - 7 days ago 5
Javascript Question

If checkboxed is checked, textbox will compute

enter image description here

As shown in the image below, I have the

Sell Price
textbox and
Use Sell Price Percentage
textbox. My
Use Sell Price Percentage
have this code

<div class="checkbox">
<label>
<input type="checkbox" name="sellpCheck" id="scheck" onclick="document.getElementById('sell_price').disabled=this.checked;">
* Use Sell Price Percentage
</label>



and here's my
Sell Price
textbox

<input type="number" class="form-control" name="sell_price" id="sell_price" step='0.01' value='0.00' placeholder='0.00' required>


lastly, my
Buy Price
textbox

<input type="number" class="form-control" name="buy_price" placeholder="Buying Price Per Unit"
step='0.01' value='0.00' placeholder='0.00' min="0" max="9999" required id="buyp">


Here's my initial jQuery code:

var buyp = parseInt($('#buyp').val());
var sellp = parseFloat($('#sellp').val());
$('#sell_price').val((buyp * ((sellp/100)+1)).toFixed(2))


What I want to happen, is when I click the
Use Sell Price Percentage
the
Sell Price
textbox will automatically compute and can also recompute if I change the buyprice onkeyup.

Answer

You would have to do something like this then,

In HTML:

 <input type="checkbox" name="sellpCheck" id="scheck" onclick="document.getElementById('sell_price').disabled=this.checked;checkThis();">

In JS:

function checkThis(){

var buyp = parseInt($('#buyp').val());
var sellp = parseFloat($('#sellp').val());
$('#sell_price').val((buyp * ((sellp/100)+1)).toFixed(2));
}

For the change in buyPrice you could use onblur:

In HTML:

 <input type="number" class="form-control" name="buy_price" placeholder="Buying Price Per Unit" 
   step='0.01' value='0.00' placeholder='0.00' min="0" max="9999" required id="buyp" onblur="checkThis()">
Comments