Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Christopher Gjelten
2,305 PointsHelp with adding items to cart in JS
When I add items to the cart and go to the cart page, only the last product i clicked is shown. Please help!!
2 Answers
Dane Parchment
Treehouse Moderator 11,077 PointsThe work space you linked to is incomplete. There are scripts that you are linking to that aren't there, and pages that you are linking to that aren't there, which is making it difficult to debug, since I can't test your code in the browser. Basically your application currently does nothing and will just give missing file and 404 errors.
As for why your cart is only showing the last product, if I were to guess I think it would be these two lines of code here:
// henter bilde og tekst
document.getElementById("utskrift-cookie-p").innerHTML = `<article id='product-handlekurv'><img src='${bilde}' alt='${h3}'/><h3>${h3}</h3></article>`;
//viser hvor mye som ligger i handlekurven
document.getElementById("antall").innerHTML = "Handlekurv";
It whenever these lines of code are run, it will overwrite itself, so if you had multiple items and this code ran multiple times, the first item would be added, then the second item would overwrite it and so on and so on, until only the last remaining element has overwritten everything else.
Your code comments appear to be in German or Norwegian, which I do not speak unfortunately so I am unsure as to what you are trying to make each method do.
Christopher Gjelten
2,305 PointsOk so, who would I write the code so it didnt overwirte, and below is a new workspace with it all.