Javascript Calculation Using DropDown List
I have a pricing table which contains some variables that depend on a dropdown list selection. The JSFIDDLE shows the desired outcome.
The desired outcome is dependable on the drop-down selection. As a selection is picked, then the following changes should take place in the table:
- The "Item Cost" should display the price selected.
- The "Total" price should be multiplied by the "Qty." to display the total shipping cost.
- The grand "Total" should be the sum of the total column that includes Product + Shipping Costs.
Here's the dropdown options:
<div class="input-group full-width">
<span class="input-group-addon main-color hidden-sm hidden-xs">$</span>
<select id="shippingmethod" class="selectpicker form-control shipping-method" name="shippingmethod" aria-label="Shipping Method" tabindex="">
<option value="" selected="">Selet a Shipping Method</option>
<option value="2.95">US Mail - $2.95</option>
<option value="3.50">Priority Mail - $3.50</option>
<option value="4.67">Fedex - $4.67</option>
</select>
</div>
The table is as follows:
<table id="product-list">
<thead>
<tr>
<th class="text-left">Item</th>
<th class="text-center">Qty.</th>
<th class="text-center">Item Cost</th>
<th class="text-center">Tax</th>
<th class="text-right">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr>
<td>Product Name</td>
<td class="qty">2</td>
<td class="item-cost">$10.00</td>
<td class="tax">$0.68</td>
<td class="total">$10.68</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr>
<td>Shipping Cost</td>
<td class="qty red">1</td>
<td class="item-cost red">$2.95</td>
<td class="tax">$0.00</td>
<td class="total red">$5.90</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr id="product-totals">
<td colspan="4"></td>
<td class="total red">$13.02</td>
</tr>
</tbody>
</table>
I would appreciate any help I can get regarding this scenario and if you can show on a jsfiddle it would be even better. Thanks.
javascript jquery twitter-bootstrap jquery-ui html-table
add a comment |
I have a pricing table which contains some variables that depend on a dropdown list selection. The JSFIDDLE shows the desired outcome.
The desired outcome is dependable on the drop-down selection. As a selection is picked, then the following changes should take place in the table:
- The "Item Cost" should display the price selected.
- The "Total" price should be multiplied by the "Qty." to display the total shipping cost.
- The grand "Total" should be the sum of the total column that includes Product + Shipping Costs.
Here's the dropdown options:
<div class="input-group full-width">
<span class="input-group-addon main-color hidden-sm hidden-xs">$</span>
<select id="shippingmethod" class="selectpicker form-control shipping-method" name="shippingmethod" aria-label="Shipping Method" tabindex="">
<option value="" selected="">Selet a Shipping Method</option>
<option value="2.95">US Mail - $2.95</option>
<option value="3.50">Priority Mail - $3.50</option>
<option value="4.67">Fedex - $4.67</option>
</select>
</div>
The table is as follows:
<table id="product-list">
<thead>
<tr>
<th class="text-left">Item</th>
<th class="text-center">Qty.</th>
<th class="text-center">Item Cost</th>
<th class="text-center">Tax</th>
<th class="text-right">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr>
<td>Product Name</td>
<td class="qty">2</td>
<td class="item-cost">$10.00</td>
<td class="tax">$0.68</td>
<td class="total">$10.68</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr>
<td>Shipping Cost</td>
<td class="qty red">1</td>
<td class="item-cost red">$2.95</td>
<td class="tax">$0.00</td>
<td class="total red">$5.90</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr id="product-totals">
<td colspan="4"></td>
<td class="total red">$13.02</td>
</tr>
</tbody>
</table>
I would appreciate any help I can get regarding this scenario and if you can show on a jsfiddle it would be even better. Thanks.
javascript jquery twitter-bootstrap jquery-ui html-table
To be able to assist you, and not write the code for you, please provide an example of the JavaScript you have attempted to use. If it's not working, we can guide you to get it working.
– Twisty
Nov 14 '18 at 22:15
Unfortunately, I have no knowledge of javascript as I only work with UX.Can you still help?
– Marcelo Martins
Nov 14 '18 at 22:29
That is unfortunate. Please review: stackoverflow.com/help/on-topic
– Twisty
Nov 14 '18 at 22:43
add a comment |
I have a pricing table which contains some variables that depend on a dropdown list selection. The JSFIDDLE shows the desired outcome.
The desired outcome is dependable on the drop-down selection. As a selection is picked, then the following changes should take place in the table:
- The "Item Cost" should display the price selected.
- The "Total" price should be multiplied by the "Qty." to display the total shipping cost.
- The grand "Total" should be the sum of the total column that includes Product + Shipping Costs.
Here's the dropdown options:
<div class="input-group full-width">
<span class="input-group-addon main-color hidden-sm hidden-xs">$</span>
<select id="shippingmethod" class="selectpicker form-control shipping-method" name="shippingmethod" aria-label="Shipping Method" tabindex="">
<option value="" selected="">Selet a Shipping Method</option>
<option value="2.95">US Mail - $2.95</option>
<option value="3.50">Priority Mail - $3.50</option>
<option value="4.67">Fedex - $4.67</option>
</select>
</div>
The table is as follows:
<table id="product-list">
<thead>
<tr>
<th class="text-left">Item</th>
<th class="text-center">Qty.</th>
<th class="text-center">Item Cost</th>
<th class="text-center">Tax</th>
<th class="text-right">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr>
<td>Product Name</td>
<td class="qty">2</td>
<td class="item-cost">$10.00</td>
<td class="tax">$0.68</td>
<td class="total">$10.68</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr>
<td>Shipping Cost</td>
<td class="qty red">1</td>
<td class="item-cost red">$2.95</td>
<td class="tax">$0.00</td>
<td class="total red">$5.90</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr id="product-totals">
<td colspan="4"></td>
<td class="total red">$13.02</td>
</tr>
</tbody>
</table>
I would appreciate any help I can get regarding this scenario and if you can show on a jsfiddle it would be even better. Thanks.
javascript jquery twitter-bootstrap jquery-ui html-table
I have a pricing table which contains some variables that depend on a dropdown list selection. The JSFIDDLE shows the desired outcome.
The desired outcome is dependable on the drop-down selection. As a selection is picked, then the following changes should take place in the table:
- The "Item Cost" should display the price selected.
- The "Total" price should be multiplied by the "Qty." to display the total shipping cost.
- The grand "Total" should be the sum of the total column that includes Product + Shipping Costs.
Here's the dropdown options:
<div class="input-group full-width">
<span class="input-group-addon main-color hidden-sm hidden-xs">$</span>
<select id="shippingmethod" class="selectpicker form-control shipping-method" name="shippingmethod" aria-label="Shipping Method" tabindex="">
<option value="" selected="">Selet a Shipping Method</option>
<option value="2.95">US Mail - $2.95</option>
<option value="3.50">Priority Mail - $3.50</option>
<option value="4.67">Fedex - $4.67</option>
</select>
</div>
The table is as follows:
<table id="product-list">
<thead>
<tr>
<th class="text-left">Item</th>
<th class="text-center">Qty.</th>
<th class="text-center">Item Cost</th>
<th class="text-center">Tax</th>
<th class="text-right">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr>
<td>Product Name</td>
<td class="qty">2</td>
<td class="item-cost">$10.00</td>
<td class="tax">$0.68</td>
<td class="total">$10.68</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr>
<td>Shipping Cost</td>
<td class="qty red">1</td>
<td class="item-cost red">$2.95</td>
<td class="tax">$0.00</td>
<td class="total red">$5.90</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr id="product-totals">
<td colspan="4"></td>
<td class="total red">$13.02</td>
</tr>
</tbody>
</table>
I would appreciate any help I can get regarding this scenario and if you can show on a jsfiddle it would be even better. Thanks.
javascript jquery twitter-bootstrap jquery-ui html-table
javascript jquery twitter-bootstrap jquery-ui html-table
asked Nov 14 '18 at 17:16
Marcelo MartinsMarcelo Martins
859
859
To be able to assist you, and not write the code for you, please provide an example of the JavaScript you have attempted to use. If it's not working, we can guide you to get it working.
– Twisty
Nov 14 '18 at 22:15
Unfortunately, I have no knowledge of javascript as I only work with UX.Can you still help?
– Marcelo Martins
Nov 14 '18 at 22:29
That is unfortunate. Please review: stackoverflow.com/help/on-topic
– Twisty
Nov 14 '18 at 22:43
add a comment |
To be able to assist you, and not write the code for you, please provide an example of the JavaScript you have attempted to use. If it's not working, we can guide you to get it working.
– Twisty
Nov 14 '18 at 22:15
Unfortunately, I have no knowledge of javascript as I only work with UX.Can you still help?
– Marcelo Martins
Nov 14 '18 at 22:29
That is unfortunate. Please review: stackoverflow.com/help/on-topic
– Twisty
Nov 14 '18 at 22:43
To be able to assist you, and not write the code for you, please provide an example of the JavaScript you have attempted to use. If it's not working, we can guide you to get it working.
– Twisty
Nov 14 '18 at 22:15
To be able to assist you, and not write the code for you, please provide an example of the JavaScript you have attempted to use. If it's not working, we can guide you to get it working.
– Twisty
Nov 14 '18 at 22:15
Unfortunately, I have no knowledge of javascript as I only work with UX.Can you still help?
– Marcelo Martins
Nov 14 '18 at 22:29
Unfortunately, I have no knowledge of javascript as I only work with UX.Can you still help?
– Marcelo Martins
Nov 14 '18 at 22:29
That is unfortunate. Please review: stackoverflow.com/help/on-topic
– Twisty
Nov 14 '18 at 22:43
That is unfortunate. Please review: stackoverflow.com/help/on-topic
– Twisty
Nov 14 '18 at 22:43
add a comment |
1 Answer
1
active
oldest
votes
Here is an example you may consider. Since you are not familiar with JavaScript, and by proxy jQuery, the answer may not help you much.
Working Example: http://jsfiddle.net/Twisty/tn3kd04o/23/
HTML
<form id="ReviewShippingAndPayment" name="ReviewShippingAndPayment" method="post" action="/Distributor/Shop/Product-List">
<div class="container col-lg-12 col-md-12 col-sm-12 col-xs-12 p0">
<div class="pl8 bbcustom p0">
<h2 class="page-header"><i class="fa fa-cube pr8 hidden-sm hidden-xs"></i>Shipping Method Selection</h2>
<p>
The desired outcome is driven by the "<span class="fw6">Select a Shipping Method</span>" dropdown.<br /><br />When a shipping method is selected, then the "Item Cost" should be multiplied by the "Qty." and displayed in the Shipping Cost Total
column.
<br /><br />Such selection should also update the final cost that include the Product Cost + Total Shipping.
</p>
<span class="spacer"></span>
</div>
<div class="spacer"></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 p0">
<div class="shipping-method-container">
<div class="field-wrapper">
<div class="input-group full-width">
<span class="input-group-addon main-color hidden-sm hidden-xs">$</span>
<select id="shippingmethod" class="selectpicker form-control shipping-method" name="shippingmethod" aria-label="Shipping Method" tabindex="">
<option value="" selected="">Selet a Shipping Method</option>
<option value="2.95">US Mail - $2.95</option>
<option value="3.50">Priority Mail - $3.50</option>
<option value="4.67">Fedex - $4.67</option>
</select>
</div>
</div>
<span class="help-block"></span>
</div>
<div class="clearfix"></div>
<hr style="margim:0px;padding:0;" />
<div class="clearfix"></div>
<table id="product-list">
<thead>
<tr>
<th class="text-left">Item</th>
<th class="text-center">Qty.</th>
<th class="text-center">Item Cost</th>
<th class="text-center">Tax</th>
<th class="text-right">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr class="prod-row">
<td>Product Name</td>
<td class="qty">1</td>
<td class="item-cost">$10.00</td>
<td class="tax">$0.68</td>
<td class="total">$10.68</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr class="ship-row">
<td>Shipping Cost</td>
<td class="qty red">1</td>
<td class="item-cost red">$2.95</td>
<td class="tax">$0.00</td>
<td class="total red">$5.90</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr id="product-totals">
<td colspan="4"></td>
<td class="total red">$13.02</td>
</tr>
</tbody>
</table>
</div>
</div>
</form>
Some minor changes to the HTML so that it's easier to address.
JavaScript
$(function()
function calcTotal()
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var s =
qty: parseInt($(".ship-row .qty").text().trim()),
tax: parseFloat($(".ship-row .tax").text().trim().slice(1)),
price: parseFloat($(".ship-row .item-cost").text().trim().slice(1))
var t = 0.00;
if (p && s)
t = (p.qty * p.price + p.tax) + (s.qty * s.price + s.tax);
console.log(p, s, t);
return t;
$("#shippingmethod").change(function(e)
var shipQty = parseInt($(".ship-row .qty").text());
var shipCost = parseFloat($(this).val());
$(".ship-row .item-cost").html("$" + shipCost.toFixed(2));
$(".ship-row .total").html("$" + (shipQty * shipCost).toFixed(2));
var total = calcTotal();
$("#product-totals .total").html("$" + total.toFixed(2));
);
);
When the drop down is changed, the change
event callback is triggered. We collect a few details and then perform some basic math functions.
Hope that helps.
This is AWESOME! You're AWESOME! Thanks!
– Marcelo Martins
Nov 14 '18 at 22:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305566%2fjavascript-calculation-using-dropdown-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is an example you may consider. Since you are not familiar with JavaScript, and by proxy jQuery, the answer may not help you much.
Working Example: http://jsfiddle.net/Twisty/tn3kd04o/23/
HTML
<form id="ReviewShippingAndPayment" name="ReviewShippingAndPayment" method="post" action="/Distributor/Shop/Product-List">
<div class="container col-lg-12 col-md-12 col-sm-12 col-xs-12 p0">
<div class="pl8 bbcustom p0">
<h2 class="page-header"><i class="fa fa-cube pr8 hidden-sm hidden-xs"></i>Shipping Method Selection</h2>
<p>
The desired outcome is driven by the "<span class="fw6">Select a Shipping Method</span>" dropdown.<br /><br />When a shipping method is selected, then the "Item Cost" should be multiplied by the "Qty." and displayed in the Shipping Cost Total
column.
<br /><br />Such selection should also update the final cost that include the Product Cost + Total Shipping.
</p>
<span class="spacer"></span>
</div>
<div class="spacer"></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 p0">
<div class="shipping-method-container">
<div class="field-wrapper">
<div class="input-group full-width">
<span class="input-group-addon main-color hidden-sm hidden-xs">$</span>
<select id="shippingmethod" class="selectpicker form-control shipping-method" name="shippingmethod" aria-label="Shipping Method" tabindex="">
<option value="" selected="">Selet a Shipping Method</option>
<option value="2.95">US Mail - $2.95</option>
<option value="3.50">Priority Mail - $3.50</option>
<option value="4.67">Fedex - $4.67</option>
</select>
</div>
</div>
<span class="help-block"></span>
</div>
<div class="clearfix"></div>
<hr style="margim:0px;padding:0;" />
<div class="clearfix"></div>
<table id="product-list">
<thead>
<tr>
<th class="text-left">Item</th>
<th class="text-center">Qty.</th>
<th class="text-center">Item Cost</th>
<th class="text-center">Tax</th>
<th class="text-right">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr class="prod-row">
<td>Product Name</td>
<td class="qty">1</td>
<td class="item-cost">$10.00</td>
<td class="tax">$0.68</td>
<td class="total">$10.68</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr class="ship-row">
<td>Shipping Cost</td>
<td class="qty red">1</td>
<td class="item-cost red">$2.95</td>
<td class="tax">$0.00</td>
<td class="total red">$5.90</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr id="product-totals">
<td colspan="4"></td>
<td class="total red">$13.02</td>
</tr>
</tbody>
</table>
</div>
</div>
</form>
Some minor changes to the HTML so that it's easier to address.
JavaScript
$(function()
function calcTotal()
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var s =
qty: parseInt($(".ship-row .qty").text().trim()),
tax: parseFloat($(".ship-row .tax").text().trim().slice(1)),
price: parseFloat($(".ship-row .item-cost").text().trim().slice(1))
var t = 0.00;
if (p && s)
t = (p.qty * p.price + p.tax) + (s.qty * s.price + s.tax);
console.log(p, s, t);
return t;
$("#shippingmethod").change(function(e)
var shipQty = parseInt($(".ship-row .qty").text());
var shipCost = parseFloat($(this).val());
$(".ship-row .item-cost").html("$" + shipCost.toFixed(2));
$(".ship-row .total").html("$" + (shipQty * shipCost).toFixed(2));
var total = calcTotal();
$("#product-totals .total").html("$" + total.toFixed(2));
);
);
When the drop down is changed, the change
event callback is triggered. We collect a few details and then perform some basic math functions.
Hope that helps.
This is AWESOME! You're AWESOME! Thanks!
– Marcelo Martins
Nov 14 '18 at 22:58
add a comment |
Here is an example you may consider. Since you are not familiar with JavaScript, and by proxy jQuery, the answer may not help you much.
Working Example: http://jsfiddle.net/Twisty/tn3kd04o/23/
HTML
<form id="ReviewShippingAndPayment" name="ReviewShippingAndPayment" method="post" action="/Distributor/Shop/Product-List">
<div class="container col-lg-12 col-md-12 col-sm-12 col-xs-12 p0">
<div class="pl8 bbcustom p0">
<h2 class="page-header"><i class="fa fa-cube pr8 hidden-sm hidden-xs"></i>Shipping Method Selection</h2>
<p>
The desired outcome is driven by the "<span class="fw6">Select a Shipping Method</span>" dropdown.<br /><br />When a shipping method is selected, then the "Item Cost" should be multiplied by the "Qty." and displayed in the Shipping Cost Total
column.
<br /><br />Such selection should also update the final cost that include the Product Cost + Total Shipping.
</p>
<span class="spacer"></span>
</div>
<div class="spacer"></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 p0">
<div class="shipping-method-container">
<div class="field-wrapper">
<div class="input-group full-width">
<span class="input-group-addon main-color hidden-sm hidden-xs">$</span>
<select id="shippingmethod" class="selectpicker form-control shipping-method" name="shippingmethod" aria-label="Shipping Method" tabindex="">
<option value="" selected="">Selet a Shipping Method</option>
<option value="2.95">US Mail - $2.95</option>
<option value="3.50">Priority Mail - $3.50</option>
<option value="4.67">Fedex - $4.67</option>
</select>
</div>
</div>
<span class="help-block"></span>
</div>
<div class="clearfix"></div>
<hr style="margim:0px;padding:0;" />
<div class="clearfix"></div>
<table id="product-list">
<thead>
<tr>
<th class="text-left">Item</th>
<th class="text-center">Qty.</th>
<th class="text-center">Item Cost</th>
<th class="text-center">Tax</th>
<th class="text-right">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr class="prod-row">
<td>Product Name</td>
<td class="qty">1</td>
<td class="item-cost">$10.00</td>
<td class="tax">$0.68</td>
<td class="total">$10.68</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr class="ship-row">
<td>Shipping Cost</td>
<td class="qty red">1</td>
<td class="item-cost red">$2.95</td>
<td class="tax">$0.00</td>
<td class="total red">$5.90</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr id="product-totals">
<td colspan="4"></td>
<td class="total red">$13.02</td>
</tr>
</tbody>
</table>
</div>
</div>
</form>
Some minor changes to the HTML so that it's easier to address.
JavaScript
$(function()
function calcTotal()
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var s =
qty: parseInt($(".ship-row .qty").text().trim()),
tax: parseFloat($(".ship-row .tax").text().trim().slice(1)),
price: parseFloat($(".ship-row .item-cost").text().trim().slice(1))
var t = 0.00;
if (p && s)
t = (p.qty * p.price + p.tax) + (s.qty * s.price + s.tax);
console.log(p, s, t);
return t;
$("#shippingmethod").change(function(e)
var shipQty = parseInt($(".ship-row .qty").text());
var shipCost = parseFloat($(this).val());
$(".ship-row .item-cost").html("$" + shipCost.toFixed(2));
$(".ship-row .total").html("$" + (shipQty * shipCost).toFixed(2));
var total = calcTotal();
$("#product-totals .total").html("$" + total.toFixed(2));
);
);
When the drop down is changed, the change
event callback is triggered. We collect a few details and then perform some basic math functions.
Hope that helps.
This is AWESOME! You're AWESOME! Thanks!
– Marcelo Martins
Nov 14 '18 at 22:58
add a comment |
Here is an example you may consider. Since you are not familiar with JavaScript, and by proxy jQuery, the answer may not help you much.
Working Example: http://jsfiddle.net/Twisty/tn3kd04o/23/
HTML
<form id="ReviewShippingAndPayment" name="ReviewShippingAndPayment" method="post" action="/Distributor/Shop/Product-List">
<div class="container col-lg-12 col-md-12 col-sm-12 col-xs-12 p0">
<div class="pl8 bbcustom p0">
<h2 class="page-header"><i class="fa fa-cube pr8 hidden-sm hidden-xs"></i>Shipping Method Selection</h2>
<p>
The desired outcome is driven by the "<span class="fw6">Select a Shipping Method</span>" dropdown.<br /><br />When a shipping method is selected, then the "Item Cost" should be multiplied by the "Qty." and displayed in the Shipping Cost Total
column.
<br /><br />Such selection should also update the final cost that include the Product Cost + Total Shipping.
</p>
<span class="spacer"></span>
</div>
<div class="spacer"></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 p0">
<div class="shipping-method-container">
<div class="field-wrapper">
<div class="input-group full-width">
<span class="input-group-addon main-color hidden-sm hidden-xs">$</span>
<select id="shippingmethod" class="selectpicker form-control shipping-method" name="shippingmethod" aria-label="Shipping Method" tabindex="">
<option value="" selected="">Selet a Shipping Method</option>
<option value="2.95">US Mail - $2.95</option>
<option value="3.50">Priority Mail - $3.50</option>
<option value="4.67">Fedex - $4.67</option>
</select>
</div>
</div>
<span class="help-block"></span>
</div>
<div class="clearfix"></div>
<hr style="margim:0px;padding:0;" />
<div class="clearfix"></div>
<table id="product-list">
<thead>
<tr>
<th class="text-left">Item</th>
<th class="text-center">Qty.</th>
<th class="text-center">Item Cost</th>
<th class="text-center">Tax</th>
<th class="text-right">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr class="prod-row">
<td>Product Name</td>
<td class="qty">1</td>
<td class="item-cost">$10.00</td>
<td class="tax">$0.68</td>
<td class="total">$10.68</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr class="ship-row">
<td>Shipping Cost</td>
<td class="qty red">1</td>
<td class="item-cost red">$2.95</td>
<td class="tax">$0.00</td>
<td class="total red">$5.90</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr id="product-totals">
<td colspan="4"></td>
<td class="total red">$13.02</td>
</tr>
</tbody>
</table>
</div>
</div>
</form>
Some minor changes to the HTML so that it's easier to address.
JavaScript
$(function()
function calcTotal()
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var s =
qty: parseInt($(".ship-row .qty").text().trim()),
tax: parseFloat($(".ship-row .tax").text().trim().slice(1)),
price: parseFloat($(".ship-row .item-cost").text().trim().slice(1))
var t = 0.00;
if (p && s)
t = (p.qty * p.price + p.tax) + (s.qty * s.price + s.tax);
console.log(p, s, t);
return t;
$("#shippingmethod").change(function(e)
var shipQty = parseInt($(".ship-row .qty").text());
var shipCost = parseFloat($(this).val());
$(".ship-row .item-cost").html("$" + shipCost.toFixed(2));
$(".ship-row .total").html("$" + (shipQty * shipCost).toFixed(2));
var total = calcTotal();
$("#product-totals .total").html("$" + total.toFixed(2));
);
);
When the drop down is changed, the change
event callback is triggered. We collect a few details and then perform some basic math functions.
Hope that helps.
Here is an example you may consider. Since you are not familiar with JavaScript, and by proxy jQuery, the answer may not help you much.
Working Example: http://jsfiddle.net/Twisty/tn3kd04o/23/
HTML
<form id="ReviewShippingAndPayment" name="ReviewShippingAndPayment" method="post" action="/Distributor/Shop/Product-List">
<div class="container col-lg-12 col-md-12 col-sm-12 col-xs-12 p0">
<div class="pl8 bbcustom p0">
<h2 class="page-header"><i class="fa fa-cube pr8 hidden-sm hidden-xs"></i>Shipping Method Selection</h2>
<p>
The desired outcome is driven by the "<span class="fw6">Select a Shipping Method</span>" dropdown.<br /><br />When a shipping method is selected, then the "Item Cost" should be multiplied by the "Qty." and displayed in the Shipping Cost Total
column.
<br /><br />Such selection should also update the final cost that include the Product Cost + Total Shipping.
</p>
<span class="spacer"></span>
</div>
<div class="spacer"></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 p0">
<div class="shipping-method-container">
<div class="field-wrapper">
<div class="input-group full-width">
<span class="input-group-addon main-color hidden-sm hidden-xs">$</span>
<select id="shippingmethod" class="selectpicker form-control shipping-method" name="shippingmethod" aria-label="Shipping Method" tabindex="">
<option value="" selected="">Selet a Shipping Method</option>
<option value="2.95">US Mail - $2.95</option>
<option value="3.50">Priority Mail - $3.50</option>
<option value="4.67">Fedex - $4.67</option>
</select>
</div>
</div>
<span class="help-block"></span>
</div>
<div class="clearfix"></div>
<hr style="margim:0px;padding:0;" />
<div class="clearfix"></div>
<table id="product-list">
<thead>
<tr>
<th class="text-left">Item</th>
<th class="text-center">Qty.</th>
<th class="text-center">Item Cost</th>
<th class="text-center">Tax</th>
<th class="text-right">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr class="prod-row">
<td>Product Name</td>
<td class="qty">1</td>
<td class="item-cost">$10.00</td>
<td class="tax">$0.68</td>
<td class="total">$10.68</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr class="ship-row">
<td>Shipping Cost</td>
<td class="qty red">1</td>
<td class="item-cost red">$2.95</td>
<td class="tax">$0.00</td>
<td class="total red">$5.90</td>
</tr>
<tr>
<td colspan="5" class="pt5"></td>
</tr>
<tr id="product-totals">
<td colspan="4"></td>
<td class="total red">$13.02</td>
</tr>
</tbody>
</table>
</div>
</div>
</form>
Some minor changes to the HTML so that it's easier to address.
JavaScript
$(function()
function calcTotal()
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var s =
qty: parseInt($(".ship-row .qty").text().trim()),
tax: parseFloat($(".ship-row .tax").text().trim().slice(1)),
price: parseFloat($(".ship-row .item-cost").text().trim().slice(1))
var t = 0.00;
if (p && s)
t = (p.qty * p.price + p.tax) + (s.qty * s.price + s.tax);
console.log(p, s, t);
return t;
$("#shippingmethod").change(function(e)
var shipQty = parseInt($(".ship-row .qty").text());
var shipCost = parseFloat($(this).val());
$(".ship-row .item-cost").html("$" + shipCost.toFixed(2));
$(".ship-row .total").html("$" + (shipQty * shipCost).toFixed(2));
var total = calcTotal();
$("#product-totals .total").html("$" + total.toFixed(2));
);
);
When the drop down is changed, the change
event callback is triggered. We collect a few details and then perform some basic math functions.
Hope that helps.
answered Nov 14 '18 at 22:47
TwistyTwisty
14.3k11534
14.3k11534
This is AWESOME! You're AWESOME! Thanks!
– Marcelo Martins
Nov 14 '18 at 22:58
add a comment |
This is AWESOME! You're AWESOME! Thanks!
– Marcelo Martins
Nov 14 '18 at 22:58
This is AWESOME! You're AWESOME! Thanks!
– Marcelo Martins
Nov 14 '18 at 22:58
This is AWESOME! You're AWESOME! Thanks!
– Marcelo Martins
Nov 14 '18 at 22:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305566%2fjavascript-calculation-using-dropdown-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
To be able to assist you, and not write the code for you, please provide an example of the JavaScript you have attempted to use. If it's not working, we can guide you to get it working.
– Twisty
Nov 14 '18 at 22:15
Unfortunately, I have no knowledge of javascript as I only work with UX.Can you still help?
– Marcelo Martins
Nov 14 '18 at 22:29
That is unfortunate. Please review: stackoverflow.com/help/on-topic
– Twisty
Nov 14 '18 at 22:43