Rafał Kokosiński Rafał Kokosiński - 4 months ago 18
Javascript Question

Skip text input value when calculating the VAT

I have simple script which calculates the VAT:

<script>
function count(){
price = document.getElementById("price").value;
vat = document.getElementById("vat").value;
total = price*(1+vat/100);
document.getElementById('total').innerHTML=total;
}
</script>

<input size="20" MAXLENGTH="20" id="price" name="price" type="text" value="" onkeyup="count();">

<select id="vat" onchange="count();">
<option VALUE="22">22 %</option>
<option VALUE="7">7 %</option>
<option VALUE="33">33 %</option>
<option VALUE="ZW">ZW</option>
</select>

<span id="total"></span>


Here is how this script

I want to do to the script, when I will choose from the selection list the value of a text (eg. "ZW"), then the script to ignore this value and the result showed only entered value in the field.

Answer

function count(){
  price = document.getElementById("price").value;  
  vat = document.getElementById("vat").value;    
  
  if (isNaN(vat))
    total=price;
  else
    total = price*(1+vat/100);
  
  
  document.getElementById('total').innerHTML=total;           
}
<input size="20" MAXLENGTH="20" id="price" name="price" type="text" value="" onkeyup="count();">

<select id="vat" onchange="count();">
<option VALUE="22">22 %</option>
<option VALUE="7">7 %</option>
<option VALUE="33">33 %</option>
<option VALUE="ZW">ZW</option>
</select>

<span id="total"></span>