You can see id=”points” in the above line, and in the javascript you have used “quantity”
var selScnd = parseInt(document.getElementById("quantity").value);
And cannot guess what the if-else is actually written for, every condition is doing the same work. So remove the if-else. Try the code below.
function calculateAmount() {
var selFrst = parseInt(document.getElementById("size").value);
var selScnd = parseInt(document.getElementById("points").value);
var tot_price = selFrst * selScnd;
/*display the result*/
document.getElementById("tot_amount").value = tot_price;
}
Solution 2 :
The id of the second div is wrong, also, you should compute the price only when both input are chosen:
function calculateAmount() {
var selFrst = parseInt(document.getElementById("size").value);
var selScnd = parseInt(document.getElementById("points").value);
var tot_price;
if(selScnd && selFrst) {
if (selFrst == 120) {
tot_price = selFrst * selScnd;
} else if (selFrst == 80) {
tot_price = selFrst * selScnd;
} else if (selFrst == 64) {
tot_price = selFrst * selScnd;
}
document.getElementById("tot_amount").value = tot_price;
}
/*display the result*/
}
Variable selScnd should be selected with the id point, not width quantity since there is no such id as quantity. In other words there is a typo in your Code.
Your code should be like this:
function calculateAmount() {
var selFrst = parseInt(document.getElementById("size").value);
var selScnd = parseInt(document.getElementById("points").value);
var tot_price;
if (selFrst == 120) {
tot_price = selFrst * selScnd;
} else if (selFrst == 80) {
tot_price = selFrst * selScnd;
} else if (selFrst == 64) {
tot_price = selFrst * selScnd;
}
/*display the result*/
document.getElementById("tot_amount").value = tot_price;
}
This code should allow users to choose the size and quantity of banners and the code will automatically calculate the price they have to pay. I tried so many ways but it failed. Below is the code that I had done
function calculateAmount() {
var selFrst = parseInt(document.getElementById("size").value);
var selScnd = parseInt(document.getElementById("quantity").value);
var tot_price;
if (selFrst == 120) {
tot_price = selFrst * selScnd;
} else if (selFrst == 80) {
tot_price = selFrst * selScnd;
} else if (selFrst == 64) {
tot_price = selFrst * selScnd;
}
/*display the result*/
document.getElementById("tot_amount").value = tot_price;
}
I try edit like you suggest but it still didn’t work
Comment posted by Subhashis Pandey
can you please tell me what the if-else is doing here?
Comment posted by swebdev
@SubhashisPandey As you can see every option in select input has a value attribute (value=”120″, value=”80″, and value=”64″), he used if-else to compare those values. and then those value are multiplied with second input/variable not 4×10 etc because we selected values of input
Comment posted by jabaa
“he used if-else to compare those values” I don’t think “SABRINA” is a “he”
Comment posted by swebdev
??is that a big deal?
Comment posted by Subhashis Pandey
instead of posting your query as answer, you should add comment or update your question.
Comment posted by Community
Please add further details to expand on your answer, such as working code or documentation citations.