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

joob

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>

Nastya Hahanova

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]")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Javascript image slider setInterval()

Javascript image slider not working properly

Darken Image Slider/Button -- Javascript

JavaScript - Image slider buttons not working

Image Slider using HTML and Javascript

JavaScript image slider not fading between images

vanilla javascript & css image slider not working properly

click on slider image to do a function in javascript

Javascript Image Slider - Need to add buttons

javascript slider arrow keys and fade image

HTML Javascript change image with Slider bar step

Image Slider not working because of wrong delay Javascript

Slider image issues when Javascript Disabled

Autoplay image slider with Jquery,javascript and css

How to get the refrence of the current image in image slider-javascript?

Disable next button in last image in image slider Javascript

How to find out the section of Javascript that affects an HTML image slider element?

JavaScript: How to add multiple image slider on one page with same script?

How to convert Image Slider Javascript code to reactjs using Hooks

javascript slider opacity effect on main image after a click on a thumbnail

How to make an image slider using JavaScript with user generated images?

How can I use javascript to make a working image slider work?

Image not showing on image slider

My JavaScript image gallery slider's right slider isn't working and my left one is

Jquery slider: horizontal image slider

How to implement android tutorial slider

Image slider (cycle-loop) onmouseover image-thumbnail, stop on mouseout in JavaScript

image slider in a row of table

Control image size with slider