Merhabalar. HTML ile bir site yazıyorum. Javascript kodumda bir hata var ama çözemedim. Bu konuda profesyonel olan arkadaşlar yardımcı olabilirler mi?
Problem şu; fiyat azaltma butonuna bastığımda fiyat arttırma butonuyla aynı işi yapıyor. Yani fiyatı arttırıyor. Bunu nasıl düzeltebilirim?
document.addEventListener('DOMContentLoaded', function() {
const decreaseBtns = document.querySelectorAll('.decrease-btn');
const increaseBtns = document.querySelectorAll('.increase-btn');
const quantityValues = document.querySelectorAll('.item-quantity');
const deleteIcons = document.querySelectorAll('.delete-icon');
const items = document.querySelectorAll('.item');
const itemPrices = document.querySelectorAll('.item-price');
const checkoutBtn = document.querySelector('.payment-btn');
const totalPriceElement = document.getElementById('total-price');
const cartItemsContainer = document.querySelector('.cart-items');
// Total price'i güncelle
updateTotalPrice();
checkoutBtn.addEventListener('click', function() {
window.location.href = 'payment.html';
});
function performSearch() {
var searchValue = document.getElementById('search-input').value;
alert('You searched for: ' + searchValue);
}
document.getElementById('search-icon').addEventListener('click', performSearch);
document.getElementById('search-input').addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
event.preventDefault();
performSearch();
}
});
decreaseBtns.forEach(function(btn, index) {
btn.addEventListener('click', function() {
let currentValue = parseInt(quantityValues[index].textContent);
if (currentValue > 1) {
quantityValues[index].textContent = currentValue - 1;
updateTotalPrice();
updateCartDropdown();
}
});
});
increaseBtns.forEach(function(btn, index) {
btn.addEventListener('click', function() {
let currentValue = parseInt(quantityValues[index].textContent);
quantityValues[index].textContent = currentValue + 1;
updateTotalPrice();
updateCartDropdown();
});
});
deleteIcons.forEach(function(icon, index) {
icon.addEventListener('click', function() {
items[index].remove();
updateTotalPrice();
updateCartDropdown();
});
});
function updateTotalPrice() {
let totalPrice = 0;
items.forEach((item, index) => {
if (item.parentElement) {
let quantity = parseInt(quantityValues[index].textContent);
let pricePerItem = parseFloat(itemPrices[index].textContent);
let itemTotal = quantity * pricePerItem;
totalPrice += itemTotal;
itemPrices[index].textContent = itemTotal.toFixed(2) + ' TL';
}
});
totalPriceElement.textContent = totalPrice.toFixed(2);
}
function updateCartDropdown() {
cartItemsContainer.innerHTML = '';
items.forEach((item, index) => {
if (item.parentElement) {
let itemName = item.querySelector('.item-details h3').textContent;
let itemPrice = item.querySelector('.item-price').textContent;
let itemImageSrc = item.querySelector('.item-info img').src;
let quantity = parseInt(quantityValues[index].textContent);
let itemHTML = `
<div class="cart-item">
<img src="${itemImageSrc}" alt="${itemName}">
<div>
<span>${itemName}</span>
<span>Price: ${itemPrice}</span>
<span>Quantity: ${quantity}</span>
</div>
</div>
` ;
cartItemsContainer.innerHTML += itemHTML;
}
});
}
document.querySelector('.checkout-button').addEventListener('click', function () {
window.location.href = 'checkout.html';
});
document.querySelector('.continue-shopping-button').addEventListener('click', function () {
window.location.href = 'cart.html';
});
// Initialize cart dropdown on page load
updateCartDropdown();
});