F
F
Flexx972019-04-04 16:58:01
JavaScript
Flexx97, 2019-04-04 16:58:01

appendChild not working?

const cartWrapper = document.querySelectorAll('.cart__wrapper'),
    cart = document.querySelector('.cart'),
    close = document.querySelector('.cart__close'),
    open = document.querySelector('#cart'),
    goodsBtn = document.querySelectorAll('.goods__btn'),
    products = document.querySelectorAll('.goods__item'),
    confirm = document.querySelector('.confirm'),
    badge = document.querySelector('.nav__badge'),
    totalCost = document.querySelector('cart__total > span'),
    titles  = document.querySelectorAll('goods__title');

function openCart() {
    cart.style.display = 'block';
    document.body.style.overflow = 'hidden';
}

function closeCart() {
    cart.style.display = 'none';
    document.body.style.overflow = '';
}

open.addEventListener('click', openCart);
close.addEventListener('click', closeCart);

goodsBtn.forEach(function(btn, i){
    btn.addEventListener('click', () => {
        let item = products[i].cloneNode(true),
        trigger = item.querySelector('button'),
        removeBtn = document.createElement('div'),
        empty = document.querySelector('.empty');

        trigger.remove();

        removeBtn.classList.add('goods__item-remove');
        removeBtn.innerHTML = '&times';
        item.appendChild(removeBtn);

        cartWrapper.appendChild(item);

cartWrapper.appendChild(item) - There is an error in this line, I can't figure out why

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Proskurin, 2019-04-04
@Flexx97

const cartWrapper = document.querySelectorAll('.cart__wrapper')

The cartWrapper will contain a list of elements that have the cart_wrapper class. Probably you wanted to write
So there will be only one element in cartWrapper.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question