/    Sign up×
Community /Pin to ProfileBookmark

How can I add Shoppingitems to cart

I’m building an E-commerce website, my code to add item to cart gives this error
”uncaught TypeError: Cannot read properties of undefined (reading ‘appendChild’)
at HTMLButtonElement.addItem” HOW CAN I SOLVE THIS?
This is my code

“`
const cartContainer = document.getElementsByTagName(“tbody”)[0];

const addToCart = document.getElementsByClassName(‘buy’);

for (var i =0; i< addToCart.length; i++){
addToCart[i].addEventListener(‘click’, addItem)
}
function addItem(event){
let btn = event.target;
let btnParent = btn.parentElement
let itemName = btnParent.children[2].innerText;
let itemImage = btnParent.children[0].src;
let itemPrice = btnParent.children[3].innerText;

let itemContainer = document.createElement(‘tr’).innerHTML = `
<td><img class=”item-image” src=${itemImage} width=”40″ alt=””></td>
<td class = “item-name”>${itemName}</h3>
</td>
<td class=”item-price”><h3>${itemPrice}</h3></td>
<td><input type = ‘number’ class = ‘num’ value = ‘1’></td>
<td class=”total-price”><h3>${itemPrice}</h3></td>
<td><button class=”delete-button” type=”button”>Remove</button></td>
`

cartContainer.appendChild(itemContainer);

}
“`

to post a comment
HTMLJavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 09.2022 — @manyorock#1641276

Please use code tags when posting code: `your code here` or triple backticks. I edited your posting accordingly.
Copy linkTweet thisAlerts:
@SempervivumJan 09.2022 — @manyorock#1641276

Use the debugger or console.log and check the value of `itemContainer</C>: It's not a DOM element as appendChild requires but the HTML you assigned.<br/>
Change your code to this and it should work:
<CODE>
`<i>
</i>let itemContainer = document.createElement('tr');
itemContainer.innerHTML =

<td><img class="item-image" src=${itemImage} width="40" alt=""></td>
<td class = "item-name">${itemName}</h3>
</td>
<td class="item-price"><h3>${itemPrice}</h3></td>
<td><input type = 'number' class = 'num' value = '1'></td>
<td class="total-price"><h3>${itemPrice}</h3></td>
<td><button class="delete-button" type="button">Remove</button></td>
;<i>
</i>
``
Copy linkTweet thisAlerts:
@toropv12Jan 10.2022 — Hello, you have to use a debugger, I used it and it worked normally, I also had a problem with it, but somehow we managed
Copy linkTweet thisAlerts:
@manyorockauthorJan 12.2022 — It worked but only when I add the card details(itemContaner) on the same page with the Products Items. But when I put the itemContainer on a different page i get the ''uncaught TypeError: Cannot read properties of undefined (reading 'appendChild')

at HTMLButtonElement.addItem" error.@Sempervivum#1641285
Copy linkTweet thisAlerts:
@SempervivumJan 14.2022 — Sorry, I forgot about this thread. When the item container is on a different page you have no direct access to it. You might store the items in local storage:

https://developer.mozilla.org/de/docs/Web/API/Window/localStorage
×

Success!

Help @manyorock spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...