Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shopping Cart - JS QUERY SELECTORS #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,34 @@ <h1>Root Cart</h1>
</td>
</tr>
<!-- Iteration 2: Add more products here -->
<tr class="product">
<td class="name">
<span>Root Ducky</span>
</td>
<td class="price">$<span>85.00</span></td>
<td class="quantity">
<input type="number" value="0" min="0" placeholder="Quantity" />
</td>
<td class="subtotal">$<span>0</span></td>
<td class="action">
<button class="btn btn-remove">Remove</button>
</td>
</tr>
</tbody>
<tfoot>
<!-- <tr class="create-product">
<tr class="create-product">
<td>
<input type="text" placeholder="Product Name" />
<input type="text" class="new-product-name" placeholder="Product Name" />
</td>
<td>
<input type="number" min="0" value="0" placeholder="Product Price" />
<input type="number" class="new-product-price" min="0" value="0" placeholder="Product Price" />
</td>
<td></td>
<td></td>
<td>
<button id="create" class="btn">Create Product</button>
</td>
</tr> -->
</tr>
</tfoot>
</table>
<p class="calculate-total">
Expand Down
82 changes: 70 additions & 12 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,98 @@
function updateSubtotal(product) {
console.log('Calculating subtotal, yey!');

//... your code goes here
const price = product.querySelector('.price span');
const quantity = product.querySelector('.quantity input');

const priceValue = parseFloat(price.innerHTML);
const quantityValue = parseFloat(quantity.value);

const subtotalValue = priceValue * quantityValue;

const subtotal = product.querySelector('.subtotal span');
subtotal.innerHTML = subtotalValue.toFixed(2);

return subtotalValue;
}

function calculateAll() {
// code in the following two lines is added just for testing purposes.
// it runs when only iteration 1 is completed. at later point, it can be removed.
const singleProduct = document.querySelector('.product');
updateSubtotal(singleProduct);
// end of test

// ITERATION 2
//... your code goes here
const products = document.getElementsByClassName('product');
let total = 0;
for (let i = 0; i < products.length; i++) {
total += updateSubtotal(products[i]);
}

// ITERATION 3
//... your code goes here
const totalValue = document.querySelector('#total-value span');
totalValue.innerHTML = total.toFixed(2);
}

// ITERATION 4

function removeProduct(event) {
const target = event.currentTarget;
console.log('The target in remove is:', target);
//... your code goes here

// Find parent and remove it:
const productRow = target.closest('.product');
productRow.parentNode.removeChild(productRow);

// Recalculate
calculateAll();
}

// ITERATION 5

function createProduct() {
//... your code goes here
const nameInput = document.querySelector('.new-product-name');
const priceInput = document.querySelector('.new-product-price');

const nameValue = nameInput.value;
const priceValue = parseFloat(priceInput.value).toFixed(2);

if (nameValue.trim() === '' || priceValue <= 0) {
alert('Please enter a valid product name and price.');
return;
}

const productRow = document.createElement('tr');
productRow.classList.add('product');

productRow.innerHTML = `
<td class="name"><span>${nameValue}</span></td>
<td class="price">$<span>${priceValue}</span></td>
<td class="quantity">
<input type="number" value="0" min="0">
</td>
<td class="subtotal">$<span>0</span></td>
<td class="action">
<button class="btn btn-remove">Remove</button>
</td>
`;

const cartBody = document.querySelector('tbody');
cartBody.appendChild(productRow);

// Add event listener to the new remove button
const removeButton = productRow.querySelector('.btn-remove');
removeButton.addEventListener('click', removeProduct);

// Clear the input fields
nameInput.value = '';
priceInput.value = 0;
}

window.addEventListener('load', () => {
const calculatePricesBtn = document.getElementById('calculate');
calculatePricesBtn.addEventListener('click', calculateAll);

//... your code goes here
const createProductBtn = document.getElementById('create');
createProductBtn.addEventListener('click', createProduct);

// Attach event listeners to existing remove buttons
const removeButtons = document.getElementsByClassName('btn-remove');
for (let i = 0; i < removeButtons.length; i++) {
removeButtons[i].addEventListener('click', removeProduct);
}
});