I need help with a JavaScript function that I don't know how to implement.
I need to make a store for a school project, and I need the price to dynamically update when adding / removing quantities.
Here is a picture of the store, the +/- button work correctly and all I just need to update the price in the add button.
I tried writing a function that will take the price below the name of the product and multiply it by the value in the quantity box but it's not working.
<button class="buttonshad buttonstyling bg-primary text-light mx-5">Add <a id="changingprice">$6.99</a></button>
Here is my JavaScript function for increasing and decreasing:
function increaseValue() {
value = parseInt(document.getElementById('number').value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('number').value = value;
}
function decreaseValue() {
value = parseInt(document.getElementById('number').value, 10);
value = isNaN(value) ? 0 : value;
value < 1 ? value = 1 : '';
value--;
document.getElementById('number').value = value;
}
Basically what I need is to update the price in the button when I add / remove quantity!
Here is an example for your inspiration.
let basePrice = 6.99;
document.querySelector("#qty").addEventListener("change", function(){
document.querySelector("#changingprice span").innerText = (basePrice * this.value).toFixed(2)
})
#qty{
width: 3em;
}
<input type="number" id="qty" min="0" value="0"/>
<button id="changingprice" class="buttonshad buttonstyling bg-primary text-light mx-5">Add $<span>0.00</span></button>