SuperSimpleDev / javascript-course

708 stars 568 forks source link

Lesson 13 #140

Open Georgeikani opened 1 month ago

Georgeikani commented 1 month ago

How do i generate the Id in product.js. Const product = [{ id: name: }]

okontg commented 4 weeks ago

use the valuable name of the dataset (productId) in the querySelector, example below.

//product quantity const quantitySelector = document.querySelector(.js-quantity-selector-${productId}).value; const covertToNumber = Number(quantitySelector);

Frimpong-123 commented 1 week ago

use the valuable name of the dataset (productId) in the querySelector, example below.

//product quantity const quantitySelector = document.querySelector(.js-quantity-selector-${productId}).value; const covertToNumber = Number(quantitySelector);

please are you sure this code works cos i have tried that so many times and i am still not getting the results

okontg commented 1 week ago

this is the HTML

and this the JavaScript const cartInputQuantity = document.querySelector(.js-cart-quantity-input-${productId}).value; let newQuantity = Number(cartInputQuantity);

actually, i don't know how you sent your code.

here is the complete code, starting from the 'save link'. the save link, i use selectAll, in the 'save link' gave it a data-set (e.g, data-product-id=${matchingProduct.id}). use the variable name you use for the dataset ( e.g, const productId = saveCartQuantity.dataset.productId;), use the variable name in your input (e.g, const cartInputQuantity = document.querySelector(.js-cart-quantity-input-${productId}).value;)

//--------------------------Javascript document.querySelectorAll('.js-save-cart-quantity') .forEach((saveCartQuantity)=>{ saveCartQuantity.addEventListener('click',()=>{ const productId = saveCartQuantity.dataset.productId;

    const cartInputQuantity = document.querySelector(`.js-cart-quantity-input-${productId}`).value;
    let newQuantity = Number(cartInputQuantity);
  });
});