Having an issue where the quantity value properly increases and decreases when clicking on the + or - button, but I also want the price to increment and decrement accordingly. I need to be able to adjust the price when clicking on the increase or decrease buttons.
https://i.sstatic.net/o6SgI.png
index.html
This is my index.html file where all of the UI elements are located.
<div class="container">
<h3>Solar terrace light with inbuilt panel</h3>
<div class="row">
<div class="col-md-6">
<img src="https://s3.ap-south-1.amazonaws.com/rzp-prod-merchant-assets/payment-link/description/pr11_hq0cy7e5cmwbzw"
class="w-75">
</div>
<div class="col-md-6 mt-3">
<div class="card p-4">
<h4>Payment Details</h4>
<div class="row mt-2">
<div class="col-3">
<p>Qty.</p>
</div>
<div class="col-9">
<div class="quantity">
<button class="btn minus-btn disabled" type="button">-</button>
<input type="text" id="quantity" value="1">
<button class="btn plus-btn" type="button">+</button>
</div>
<!--will calculate price---->
</div>
</div>
<div class="row mt-2">
<div class="col-3">
<p>Name</p>
</div>
<div class="col-9">
<input type="text" class="w-100">
</div>
</div>
<div class="row">
<div class="col-3">
<p>Email</p>
</div>
<div class="col-9">
<input type="text" class="w-100">
</div>
</div>
<div class="row">
<div class="col-3">
<p>Phone</p>
</div>
<div class="col-9">
<input type="text" class="w-100">
</div>
</div>
<div class="row">
<div class="col-3">
<p>Price</p>
</div>
<div class="col-9">
<p class="total-price">
<span><i class="fas fa-rupee-sign" style="color: black;"></i></span>
<input id="price" class="text-dark" value="12,999">
</p>
</div>
</div>
<button class="btn btn-primary">Buy Now</button>
</div>
</div>
</div>
</div>
index.js
This is my index.js file where all the logic for incrementing and decrementing is implemented.
document.querySelector(".minus-btn").setAttribute("disabled", "disabled");
//taking value to increment decrement input value
var valueCount
//taking price value in variable
var price = document.getElementById("price").innerText;
//price calculation function
function priceTotal() {
var total = valueCount * price;
document.getElementById("price").innerText = total
}
//plus button
document.querySelector(".plus-btn").addEventListener("click", function () {
//getting value of input
valueCount = document.getElementById("quantity").value;
//input value increment by 1
valueCount++;
//setting increment input value
document.getElementById("quantity").value = valueCount;
if (valueCount > 1) {
document.querySelector(".minus-btn").removeAttribute("disabled");
document.querySelector(".minus-btn").classList.remove("disabled")
}
//calling price function
priceTotal()
})
//plus button
document.querySelector(".minus-btn").addEventListener("click", function () {
//getting value of input
valueCount = document.getElementById("quantity").value;
//input value increment by 1
valueCount--;
//setting increment input value
document.getElementById("quantity").value = valueCount
if (valueCount == 1) {
document.querySelector(".minus-btn").setAttribute("disabled", "disabled")
}
//calling price function
priceTotal()
})