I would like to create a calculator for a recipe that multiplies the quantity based on the number of people. The current script does some math and updates the spans with the quantity, but it comes up with something really weird.
First field I Define the amount of people (standard 4 people in the recipe)
The next fields contain the quantity of something (standard quantity for 4 people):
I suspect that one problem is the class "quantity" that exists several times. I have somehow tried to get the effective value out of every single span quantity class with .each(), but that just doesn't work.
jQuery(document).ready(function($) {
$("#people").change(function() {
$people = $(this).val();
$quantity = parseFloat($(".quantity").text());
$(".quantity").html($people * $quantity);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" value="4" id="people">
<p><span class="quantity">4</span> pieces of something</p>
<p><span class="quantity">200</span> g of butter</p>
<p><span class="quantity">500</span> ml of milk</p>