Reputation: 57
I want to create a VAT calculator where the user enters an amount and the amount is multiplied by 1.15 (assume VAT is 15%) and alert message displays the results. So if the user enters an amount of £500, then the alert message should display £575.
function mult(x) {
if (x.length == 0) {
alert("Numbers cannot be blank");
return;
}
if (isNaN(x)) {
alert("Value entered is not numeric");
return;
}
var result = parseInt(x) * 1.15;
alert("Multiplication: " + result);
}
<h1>VAT Calculator</h1><br> Amount(£): <input type="text" id="amount" name="txt_amount">
<br><br>
<button onclick="calculateVAT(amount.value)">Calculate Total Amount</button>
The code of for the javascript is not working.Can you please help me. Thanks.
Upvotes: 2
Views: 4246
Reputation: 46
The error is occurring because when the button is clicked, the code is looking for a function called calculateVAT() which does not exist in the JS.
To resolve the issue, either rename your function called mult() to calculateVAT() or change onClick in the button tag to call the mult() function rather then calculateVAT()
Calculate Total Amount
Upvotes: 1
Reputation: 17637
Rename your function from mult
to function calculateVAT(x)
function calculateVAT(x) {
if (x.length == 0) {
alert("Numbers cannot be blank");
return;
}
if (isNaN(x)) {
alert("Value entered is not numeric");
return;
}
var result = parseInt(x) * 1.15;
alert("Multiplication: " + result);
}
<h1>VAT Calculator</h1><br> Amount(£): <input type="text" id="amount" name="txt_amount">
<br><br>
<button onclick="calculateVAT(amount.value)">Calculate Total Amount</button>
Upvotes: 1