Flickity: There was an error due to the inability to read the property 'querySelectorAll' of null

I've spent hours debugging my JavaScript code, but I can't seem to get rid of this error. Did I overlook something or add incorrect code? The error message reads:

consultationSlider.js:9 Uncaught TypeError: cellsButtons.querySelectorAll is not a function at consultationSlider (consultationSlider.js:9) at HTMLDocument. (index.js:16)

Below are the snippets of my code:

JavaScript

const consultationSlider = () => {
  
  const elem = document.querySelector('.consultation-slider');
  const cellsButtonGroup = document.querySelector('.consultation-slider__nav-wrap');
  const cellsButtons = cellsButtonGroup.querySelectorAll('.consultation-slider__nav'); 

  const flkty = new Flickity( elem, {
    pageDots: false,
    wrapAround: true,
    imagesLoaded: true,
    cellAlign: 'center'
  });

  for (const [i, cellsButton] of cellsButtons.entries()) {
    cellsButton.addEventListener('click', function(event) {
      flkty.select( i );
    })
  }

 if (typeof NodeList.prototype.forEach !== 'function')  {
    NodeList.prototype.forEach = Array.prototype.forEach;
  }
}

export default consultationSlider;

SCSS

.consultation-slider {
  width: getvw(1200px);
  height: getvw(800px);

  ...remaining CSS code...

PUG

.consultation-slider
  img.consultation-slider__cell(src='/img/gray-bg-small.jpg')
  img.consultation-slider__cell(src='/img/gray-bg-small.jpg')
  
...remaining PUG code...

Answer №1

My script is running too early, before the necessary HTML elements are loaded onto the page. This results in querying for non-existing elements and getting a null result. To fix this issue, I made sure to run my code only after the DOM has fully loaded by adding an if statement:

const consultationSliderInit = () => { const sliderElement = document.querySelector('.consultation-slider');

if (sliderElement) { const flickitySlider = new Flickity(sliderElement, { pageDots: false, wrapAround: true, imagesLoaded: true, cellAlign: 'center' }); const navWrap = document.querySelector('.consultation-slider__nav-wrap'); const navButtons = navWrap.querySelectorAll('.consultation-slider__nav'); for (const [index, button] of navButtons.entries()) { button.addEventListener('click', function(event) { flickitySlider.select(index); }) } } }

export default consultationSliderInit;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

extract various information from a csv file

Having trouble reading items from a CSV file and adding them to an input form? When using a specific option value that allows you to add items by pressing comma, it doesn't work when reading from the .csv file. What could be causing this issue? with ...

Unraveling cryptic error messages in Google Closure-compiled JavaScript stack traces

We've developed a reporting system that automatically captures and organizes error stack traces from a dynamic web application built with Google Closure. Currently, I possess the source maps stored on the server and am seeking a straightforward metho ...

Trouble with the JQuery event listener onchange()

On my webpage, I've set up a drop-down list and a text-box in the following HTML code: <table> <tr> <td>Group Name: </td> <td><%= Html.Dr ...

Using Vue component with v-model and custom input handler

I'm currently working on creating a wrapper component for an <input/> element in Vue.js. Here is the component code: <template> <div> <input v-bind="$attrs" :value="value" @input="input" /> ... </div> <te ...

Issue with Angular ng-repeat not updating after array push

Struggling to grasp Angular concepts such as scopes has been a challenge for me. Recently, I encountered an issue where ng-repeat was not updating after adding a new 'wire' to my array. I suspected it could be due to the array being out of scope ...

Using @Input to pass data from a parent component to a

Looking to modularize the form code into a separate component for reusability? Consider using @Input and referencing it in the HTML to pass values to the post method. Here's how you can achieve this: Previously, everything worked smoothly when all th ...

Using an AJAX post request will result in receiving an HTML element, especially when an attachment is included with Paperclip

I've created an innovative application where users can share their stories through ajax submissions and engage with other readers by commenting on the stories using ajax as well. The technology stack I'm working with includes Rails 3.0.3, ruby 1. ...

Utilizing Vue.js to track and navigate through browsing history using the browser's

I'm currently working on an application using Vue and Laravel. Instead of relying on vue-router, Laravel is managing my routes. Within my parent component, I am dynamically loading components based on the state of an object in the data. One of the m ...

Issues encountered while utilizing Bliss as the template engine in NodeJS/Express

Seeking assistance in transitioning from Jade to Bliss as the template engine for a basic Express web application on NodeJS. Here is the code snippet from app.js: var express = require('express'), routes = require('./routes'), ...

Generating dynamic HTML content in React using a variable within a string

Currently, I am utilizing TypeScript in a React application. I am receiving the following data from an API: a) a list of variable names ["name", "surname"] b) several strings in simple HTML form with variables "<p>Hello, ho ...

Verifying code inserted into a form field

I'm looking to include a textarea on my website for users to input and save an entire script. My main concern is the possibility of users trying to insert malicious code into this textarea field. This is what the script format will look like, with t ...

The Vuex commit has exceeded the maximum calstack size limit

Currently facing an issue https://i.sstatic.net/l1WWH.png The error seems to be isolated to this specific section mounted() { this.$nextTick(() => { let ctx = this.$refs.canvas.getContext('2d') let { chartType, dataOptions ...

Arrange information into sections

This Angular code is used to format text into profile page as blocks of data: <div class="element-box"> <div class="details-wrapper"> <p><b class="label">Remote IP</b>{{apiattempt.remote_ip}}</p> <p>< ...

Tips for referencing a function declared within a prototype

I have been attempting to enhance a web page by adding functionality using a jquery library that lacks documentation. The problem I am encountering is primarily due to my lack of understanding of the jquery plugin model and/or the inner workings of javascr ...

Offspring component fails to reverse the skew as intended

Is it possible to skew a navigation bar without affecting the text inside it? I tried skewing the main div of the navigation bar and using a negative skew value on the text, but it didn't work as expected. How can I achieve this effect successfully? ...

Issue: The DLL initialization routine failed for electron, but it works perfectly fine on node.js

Currently, I am facing an issue while attempting to load a custom module in electron that is written in D using the node_dlang package. The module loads successfully with node, but encounters failures within electron. The test run with node, which works w ...

What could be causing the malfunction of the .setAttribute() and .removeAttribute functions in my JavaScript program?

Below is a flashcard game I have developed: Once a user enters the correct answer in the text box, "Correct!" is expected to be displayed in bold green font. The CSS attributes are then supposed to be removed shortly after. Here is the HTML code for the i ...

Running jQuery before page load: Tips and techniques

My website contains numerous images and scripts, however the scripts can only run after the entire page has finished loading. Is there a way to speed up this process? ...

Occasionally, my website decides to overlook the CSS stylesheet I have diligently created

While working on my web application using HTML, CSS, and PHP, I've encountered an issue where changes made to the CSS file do not reflect on the website. Even after completely deleting the CSS file, the page remains unaffected in the browser. To work ...

The MaterialTable is unable to display any data

After calling the fetch function in the useEffect, my getUsers function does not populate the data variable. I am unable to see rows of data in the MaterialTable as the data structure is in columns. I need help figuring out what I'm doing wrong. func ...