Search code examples
javascriptnullslidertypeerror

Javascript image slider tutorial: "TypeError: slides is null"


I have followed a tutorial to make an image slider and have everything copied to a T (or so I thought) but in the tutorial it works at this point where-as mine does nothing.

Developer tools debugger flags the error "TypeError: slides is null", with reference to line 10, line 4 and line 3.

I've triple checked that I haven't made any typos and that it's all copied out exactly as in the tutorial, but it's still not working.

I'm new to Javacsript and I need to get something like this working for a project and I'm in over my head so I sincerely apologise if this is a silly question. Any help greatly appreciated.

const buttons = document.querySelectorAll("[data-carousel-button]")

buttons.forEach(button => {
    button.addEventListener("click", () => {
        const offset = button.dataset.carouselButton === "next" ? 1 : -1
        const slides = button
            .closest("[data-carousel]")
            .querySelector("data-slides")

        const activeSlide = slides.querySelector("[data-active]")
        let newIndex = [...slides.children].indexOf(activeSlide) + offset
        if (newIndex < 0) newIndex = slides.children.length - 1
        if (newIndex >=  slides.children.length) newIndex = 0

        slides.children[newIndex].dataset.active = true
        delete activeSlide.dataset.active
    })
})
* *::before, *::after {
    padding:0;
    margin:0;
    box-sizing: border-box;
}

.carousesl {
    width: 100vw;
    height: 100vh;
    position: relative;
}

.slide {
    position: absolute;
    inset: 0;
    opacity: 0;
}

.slide > img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit:cover;
    object-position: center;

}

.slide[data-active] {
    opacity:1;
}

.carousel-button {
    position: absolute;
    background: none;
    border: none;
    font-size: 4rem;
    top: 50%;
    transform: translateY(-50%);
    z-index: 2;
    color: rgba(255, 255, 255, .5);
    cursor: pointer;
    border-radius: .25rem;
    padding: 0.5rem;
    background-color: rgba(0, 0, 0, .1);
}

.carousel-button:hover,
.carousel-button:focus {
    color: white;
    background-color: rgba(0, 0, 0, .2);
}

.carousel-button.next {
    left: 1rem;
}
.carousel-button.prev {
    right: 1rem;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JS Image Slider</title>
    <link rel="stylesheet" href="styles.css">
    <script src="script.js" defer></script>
</head>
<body>
    <section aria-label="Project Photos">
        <div class="carousel" data-carousel>
            <button class="carousel-button prev" data-carousel-button="prev">&#x1F86A;</button>
            <button class="carousel-button next" data-carousel-button="next">&#x1F868;</button>
            <ul data-slides>
                <li class="slide" data-active>
                    <img src="https://images.unsplash.com/photo-1653629154029-265d18f0e1f5?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1740&q=80" alt="Nature image #1">
                </li>
                <li class="slide">
                    <img src="https://images.unsplash.com/photo-1629447236132-22c57cd0f0bf?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1932&q=80" alt="Nature image #2">
                </li>
                <li class="slide">
                    <img src="https://images.unsplash.com/photo-1653161926463-725f4b39a739?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1742&q=80" alt="Nature image #3">
                </li>
                <li class="slide">
                    <img src="https://images.unsplash.com/photo-1653422064161-a2e82924adbc?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1932&q=80" alt="Nature image #4">
                </li>
                <li class="slide">
                    <img src="https://images.unsplash.com/photo-1629447236132-22c57cd0f0bf?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1932&q=80" alt="Nature image #5">
                </li>
            </ul>
        </div>
    </section>
</body>
</html>


Solution

  • It is because you forgot to add brackets.

    Instead of:

    const slides =
    button.closest("[data-carousel]").querySelector("data-slides")
    

    Use:

    const slides = button.closest("[data-carousel]").querySelector("[data-slides]")