skip to Main Content

Hello i am developing slider on javaScript. At first i am creating 3 cards on init then on click to the rigth arrow i c am creating next 3 cards and previous cards need to be removed. so i using a removeChild(slider.childNodes[i]) hovewer it deleting only new cards that i add and leaves old ones, also i tried slider.removeChild(slider.firstChild and lastChild) bu it didnt help eather. here is the code

import { pets } from '/js/pets-list.js';

const arrowLeft = document.getElementById('arrow-left');
const arrowRight = document.getElementById('arrow-right');
const mobArrowLeft = document.getElementById('mobile-arrow-left');
const mobArrowRight = document.getElementById('mobile-arrow-right');

const slider = document.getElementById('slider');

let sliderPositionLeft = slider.style.left;

for (let i = 0; i < 3; i++) {
    let newCardR = document.createElement('div');
    newCardR.className = 'slider-content_card';

    let newCardImg = document.createElement('img');
    newCardImg.src = 'assets/modal-images/woody.png';
    newCardImg.alt = 'slider-pet-image';

    let newCardText = document.createElement('p');
    newCardText.textContent = 'Woody';

    let newCardButton = document.createElement('button');
    newCardButton.className = 'button_secondary';
    newCardButton.textContent = 'Learn more';

    newCardR.appendChild(newCardImg);
    newCardR.appendChild(newCardText);
    newCardR.appendChild(newCardButton);

    slider.appendChild(newCardR);
}

let clickCounterR = 0;

arrowRight.addEventListener('click', () => {
    clickCounterR++;
    console.log(slider.childNodes);
    for (let j = 0; j < 3; j++) {
        debugger;
        let newCardR = document.createElement('div');
        newCardR.className = 'slider-content_card';

        let newCardImg = document.createElement('img');
        newCardImg.src = 'assets/modal-images/woody.png';
        newCardImg.alt = 'slider-pet-image';

        let newCardText = document.createElement('p');
        newCardText.textContent = 'Woody';

        let newCardButton = document.createElement('button');
        newCardButton.className = 'button_secondary';
        newCardButton.textContent = 'Learn more';

        newCardR.appendChild(newCardImg);
        newCardR.appendChild(newCardText);
        newCardR.appendChild(newCardButton);

        slider.appendChild(newCardR);
    }
    if (clickCounterR === 2) {
        debugger;
        for (let i = 0; i < 3; i++) {
            slider.removeChild(slider.firstChild);
        }
        clickCounterR = 0;
    }
    sliderPositionLeft = sliderPositionLeft - 1004;
    slider.style.left = sliderPositionLeft + 'px';
});

2

Answers


  1. Use

    elementToRemove.remove();
    

    With element.remove() rather than parent.removeChild(element), you won’t have to worry about applying the method on the parent element or using correct indices.

    Login or Signup to reply.
  2. Since your code is removing the new elements first, you should try removing the old cards first, and then add the new cards (so, switch your two for loops around):

    arrowRight.addEventListener('click', () => {
        clickCounterR++;
        console.log(slider.childNodes);
        if (clickCounterR === 2) {
            debugger;
            for (let i = 0; i < 3; i++) {
                slider.removeChild(slider.firstChild);
            }
            clickCounterR = 0;
        }
        for (let j = 0; j < 3; j++) {
            debugger;
            let newCardR = document.createElement('div');
            newCardR.className = 'slider-content_card';
    
            let newCardImg = document.createElement('img');
            newCardImg.src = 'assets/modal-images/woody.png';
            newCardImg.alt = 'slider-pet-image';
    
            let newCardText = document.createElement('p');
            newCardText.textContent = 'Woody';
    
            let newCardButton = document.createElement('button');
            newCardButton.className = 'button_secondary';
            newCardButton.textContent = 'Learn more';
    
            newCardR.appendChild(newCardImg);
            newCardR.appendChild(newCardText);
            newCardR.appendChild(newCardButton);
    
            slider.appendChild(newCardR);
        }
        sliderPositionLeft = sliderPositionLeft - 1004;
        slider.style.left = sliderPositionLeft + 'px';
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search