-
-
Save lukashavrlant/f71f87019bcccd405be1dce46595c940 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form> | |
<p>Cena knihy: <span id="item-price">440</span>,- Kč s DPH</p> | |
<input type="number" name="item-count" value="1" min="1" step="1"> <!-- selectovat přes name --> | |
<p>Zboží celkem: <span id="item-count-price"></span> | |
<br> | |
<input type="radio" name="delivery-option" value="(+ 99,- Kč) Dobírkou (Česká pošta)" checked>(+ 99,- Kč) Dobírkou (Česká pošta)<br> | |
<input type="radio" name="delivery-option" value="(+ 0,- Kč) Osobní odběr v Galerii Gottfrei, Krnovská 14/13, Opava">(+ 0,- Kč) Osobní odběr v Galerii Gottfrei, Krnovská 14/13, Opava | |
<p>Cena celkem: <span id="order-total"></span>,- Kč</p> | |
</form> | |
<script | |
src="http://code.jquery.com/jquery-3.3.1.min.js" | |
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" | |
crossorigin="anonymous"></script> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
renderPrice(); | |
$("input[name=item-count]").bind('keyup input change', renderPrice); | |
$("input[name=delivery-option]").change(renderPrice); | |
}); | |
function renderPrice() { | |
var subTotalPrice = getSubtotalPrice(); | |
var totalPrice = getTotalPrice(subTotalPrice); | |
$("#item-count-price").text(subTotalPrice.toString()); | |
$("#order-total").text(totalPrice.toString()); | |
} | |
function getSubtotalPrice() { | |
var bookPrice = parseInt($("#item-price").text()); | |
var bookCount = parseInt($("input[name=item-count]").val()) | |
var normalizedBookCount = Math.max(bookCount || 0, 0); | |
return bookPrice * normalizedBookCount; | |
} | |
function getTotalPrice(subtotalPrice) { | |
var shippingPriceText = $("input[name=delivery-option]:checked").val() | |
var shippingPrice = extractShippingPrice(shippingPriceText); | |
return shippingPrice + subtotalPrice; | |
} | |
function extractShippingPrice(shippingText) { | |
var defaultPrice = 0; | |
var parts = shippingText.toLowerCase().match(/[0-9]+|kč/g); | |
for (var i = 0; i < parts.length; i++) { | |
if (parts[i] === 'kč') { | |
return parseInt(parts[i - 1], 10) || defaultPrice; | |
} | |
} | |
return defaultPrice; | |
} | |
function replaceAll(target, search, replacement) { | |
return target.replace(new RegExp(search, 'g'), replacement); | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment