I am studying html, css, and js.
To add a click event to the arrow button of flickity
,
I used addEventListener. However, the following error occurred.
TypeError: Cannot read properties of null (reading 'addEventListener')
at index.js:3:6
I am still a beginner, so I do not know what is wrong.
html
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://unpkg.com/flickity@2/dist/flickity.min.css">
</head>
<body>
<h1>My Calendar</h1>
<div class="header">
<div class="carousel" data-flickity='{ "pageDots": false }'>
<div class="carousel-cell">1</div>
<div class="carousel-cell">3</div>
<div class="carousel-cell">4</div>
<div class="carousel-cell">5</div>
</div>
</div>
<div class="container">
</div>
<script src="https://unpkg.com/flickity@2/dist/flickity.pkgd.min.js"></script>
<script src="index.js"></script>
</body>
css
.carousel-cell {
width: 100%;
font-size: 48px;
background: transparent;
text-align: center;
}
.carousel .flickity-prev-next-button {
width: 100px;
height: 50px;
background: transparent;
border-radius: 0;
border: none;
box-shadow: none;
}
.carousel .flickity-prev-next-button.previous {
left: 250px;
}
.carousel .flickity-prev-next-button.next {
right: 250px;
}
javascript
const btn = document.querySelector(".carousel .flickity-prev-next-button");
btn.addEventListener("click", () => {
console.log("Hello")
});