Same question more else if() statements causes it to stop working.
I'm building a custom order form for my ad agency where the user selects the items they want from a form checkbox and the subtotal/order summary table below updates accordingly. The client is required to get at least the base spot which is $1125, while the four secondary spots are optional and are $450. If the client decides to get extra[s], then the trafficking cost also goes up ($75 each).
Everything works properly until i get to the highest amount, and then then the table doesn't update properly.
updated fiddle with full problem: http://jsfiddle.net/joelslevy21/5v8Ltkxf/3/
updated script:
if ( totalFinal == "3,330" ) {
$('#companion').css("display","block");
$('#quantityCompanion').text("4");
$('#subTotalCompanion').text("$1,800");
$('#quantityTrafficking').text("5");
$('#subTotalTrafficking').text("$375");
} else if ( totalFinal == "2,775" ) {
$('#companion').css("display","block");
$('#quantityCompanion').text("3");
$('#subTotalCompanion').text("$1,350");
$('#quantityTrafficking').text("4");
$('#subTotalTrafficking').text("$300");
} else if ( totalFinal == "2,250" ) {
$('#companion').css("display","block");
$('#quantityCompanion').text("2");
$('#subTotalCompanion').text("$900");
$('#quantityTrafficking').text("3");
$('#subTotalTrafficking').text("$225");
} else if ( totalFinal == "1,725" ) {
$('#companion').css("display","block");
$('#quantityCompanion').text("1");
$('#subTotalCompanion').text("$450");
$('#quantityTrafficking').text("2");
$('#subTotalTrafficking').text("$150");
} else {
$('#companion').css("display","none");
$('#quantityTrafficking').text("1");
$('#subTotalTrafficking').text("$75");
}
Your Trafficking
is always staying at 2
/$80
, as you are setting it to that due to your if
/else
/if
/else
block
if ( totalFinal == "805" ) {
$('#companion').css("display","block");
$('#quantityTrafficking').text("2"); // Sets to 2
$('#subTotalTrafficking').text("$80"); // Sets to $80
} else {
$('#companion').css("display","none");
$('#quantityTrafficking').text("1"); // Ignored due to other else
$('#subTotalTrafficking').text("$40"); // Ignored due to other else
}
if ( totalFinal == "1,095" ) {
$('#companion').css("display","block");
$('#quantityCompanion').text("2");
$('#subTotalCompanion').text("$500");
$('#quantityTrafficking').text("3"); // Sets to 3
$('#subTotalTrafficking').text("$120"); // Sets to $120
} else {
$('#quantityTrafficking').text("2"); // Sets to 2, as it overwrites other else
$('#subTotalTrafficking').text("$80"); // Sets to $80, as it overwrites other else
$('#quantityCompanion').text("1");
$('#subTotalCompanion').text("$250");
}
What you want is an if
/elseif
/else
block
if ( totalFinal == "1,095" ) {
$('#companion').css("display","block");
$('#quantityCompanion').text("2");
$('#subTotalCompanion').text("$500");
$('#quantityTrafficking').text("3");
$('#subTotalTrafficking').text("$120");
} else if ( totalFinal == "805" ) {
$('#companion').css("display","block");
$('#quantityCompanion').text("1");
$('#subTotalCompanion').text("$250");
$('#quantityTrafficking').text("2");
$('#subTotalTrafficking').text("$80");
} else {
$('#companion').css("display","none");
$('#quantityTrafficking').text("1");
$('#subTotalTrafficking').text("$40");
}