Currently, I am developing the functionality for a virtual store's shopping cart. At the core of this feature lies a Cart
class that manages data and executes calculations. This specific class does not interact with the HTML document or the DOM; its primary purpose is to perform computations.
The Cart
class contains methods to handle products. Each product possesses SKU, title, and price properties. I am endeavoring to introduce a quantity property as well, but it seems to be malfunctioning. Two buttons, namely addButton
and subtractButton
, are available for each product to increase or decrease their respective quantities.
My objectives include:
- Upon clicking the
addButton
, the product's quantity should increment by 1, thereby updating the total price for that SKU in thetotal-SKU
span. - Once the
subtractButton
is clicked, the product's quantity should decrement by 1 (if greater than 0), followed by an update to the total price for that SKU within thetotal-SKU
span. - The cumulative value of all SKUs ought to be computed and displayed within the
total
div.
For those interested, here is the code on jsFiddle: https://jsfiddle.net/mlpz2/edfjLhb2
The code snippet is provided below:
insert your unique rewrite of the script.js file here...
insert your unique rewrite of the styles.css file here...
The product information is retrieved from an API. The mock API utilized is jsonblob.com().
insert your unique rewrite of the JSON data here...
I seem to be grappling with the following challenges:
- Incorporating new elements into the DOM for displaying the product list.
- Detecting events to adjust the unit quantity for each product.
- Refreshing the total price on the DOM whenever modifications occur.
I am unsure about how to create event listeners for the addButton
and subtractButton
or how to manage updating the quantity and total price on the DOM. Any guidance on enhancing the code would be sincerely appreciated!
Thank you kindly for your attention :)