Having issues with the querySelectorAll function in my code

I am experiencing an issue with querySelectorAll.

Below is my code snippet:

$(window).load(
    function() {

        // Add animations
        var wwa = document.querySelectorAll(".wwa-box");

        if (window.innerWidth > 992) {
            wwa.classList.add("bounceIn");
        }

        // WOW init
        new WOW().init();

    }
);

Console Output:

TypeError: wwa.classList is undefined

I aim to assign the class .bonuceIn to all elements with the class name .wwa-box.

Answer №1

The reason behind this is that the variable wwa is a collection of elements. To apply a certain class to each element in the collection, you can use a simple loop as shown below. Take a look at this example on jsfiddle

for (let i = 0; i < wwa.length; i++){
    wwa[i].classList.add("bounceIn");
}

Answer №2

The issue arises because the variable wwa is a type of NodeList. If there is just one element with the class wwa-box, you can utilize

document.querySelector(".wwa-box");
to retrieve only the first element. Alternatively, you can loop through using foreach if there are multiple elements with the class .wwa-box.

Answer №3

Since it is a nodeList collection, you must iterate through each item individually in order to set the classList. So ensure to loop through all of them and apply the necessary modifications.

var boxes = document.querySelectorAll(".wwa-box");
for (var j=0; j<boxes.length; j++) {
     boxes[j].classList.add("bounceIn");
}

Answer №4

According to the console:

There seems to be a TypeError: wwa.classList is undefined

Is this property actually associated with wwa? Perhaps it would be beneficial to insert a debugger and explore its properties?

Referencing the alternative solution, proceed to the subsequent index and employ a for loop to navigate and assign the class.

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

Running 'ionic serve' is successful, but encountering building errors

While attempting to transpile for Ionic build, it encountered a failure with the following message: [09:56:34] build dev failed: Maximum call stack size exceeded. The ionic serve task displays a message when executed, but triggering a new transpile thr ...

It is impossible to add a new element between two existing elements that share the same parent

I'm attempting to place an <hr> tag between the first and second .field-container. Because they have the same parent, I thought using element1.parentNode.insertBefore(element2, ...) would be the solution. However, it is not working as expected a ...

When trying to pass context to the interactive node shell, an error message may appear stating "TypeError: sandbox argument must be converted to a context"

I am trying to initiate an interactive node shell with pre-initialized objects. But when I use the code below, it gives me an error: var repl = require('repl') var x = 11, y = 21 var con = {} con.x = x con.y = y repl.start('> &apo ...

Tips for utilizing a jQuery selector within nested HTML structures

Here is a snippet of my customized HTML code: <select id="my-select"> <option value="1100">Option A</option> <option value="1200">Option B</option> <option value="1300">Option C</option> </select> Rec ...

Increase the jQuery Array

After successfully initializing an Array, how can I add items to it? Is it through the push() method that I've heard about? I'm having trouble finding it... ...

Can Authorization be Combined with Filtering in a Node.js RESTful API?

In my current setup, I have a web application communicating with a RESTful API to interact with the database. The frontend of the web app uses Angular HTTP to post/get/put data to the backend, which then manages authentication, interacts with the API, and ...

In PHP/HTML, check if a tab already exists before creating a new one

Currently, I am using PHP to retrieve a list of users from the database and have included a link with them. My goal is to have the linked file open in a new tab, but using target="_blank" causes a new tab to open every time I click on the link. However, I ...

Guide to generating a dynamic array using the value of a checkbox data attribute

Whenever a checkbox is clicked, I want to create a dynamic array using its data-attribute value and then add all checked checkboxes with the same data-attribute value to this array. For example, if I click on "height," I should create an array with its da ...

What is the process for moving an ISO date from one system to

Need help with converting ISO date format? Error: Time value is not valid updateDate = e => { let inputDate = e.target.value const parts = inputDate.split('/') const rearranged = [parts[1], parts[0], parts[2]] const newDate ...

Storing a function value in a variable within a Node.js server to retrieve folder size

I have been searching for a way to determine the size of a folder's contents and discovered that using the get-folder-size package is the best option. The code snippet below explains how to achieve this: const express = require('express'); ...

Turning a Python string into a JavaScript string in Django and displaying it on a webpage

Working on my Django project, I encountered an issue in my views where I am passing a string of HTML to the page: <p><code>202</code> </p><p>hello world/p> <ul> <li>Goodbye<strong><em>next</em> ...

How can I create a dynamic form with AJAX, PHP, JavaScript, and HTML?

If you come across a situation where you have the following files: <head></head> <body> <div id="my_personal_div"> </div> </body> And in your Javascript file: $.(document).ready(){ $.ajax({ url:/any ...

Setting the || operator with JSX in React can be done by using the logical OR

Hi, I'm trying to configure two conditional statements using React. Here is the code I have: <div> {item?.status === "One" || (item?.status === "Two" && ( <Button btn="primary" title="One text" /> ))} &l ...

What is the best way to implement an 'onKeyPress' event listener for a <canvas> element in a React application?

I've been working with React for a while now and I understand how its event system functions. However, I've run into an issue where the onKeyPress event doesn't seem to be triggering on a <canvas> element. Surprisingly, it's not w ...

Protractor's browser.wait function is not functioning properly when conducting tests on a non-AngularJS website

I am currently working on testing a non-angular JS website using Protractor. Even though my test case passes successfully, I am looking to eliminate the sleep statement and replace it with either a wait or Expected condition in my test case. Here is a sni ...

What is causing the issue with Vue.js :class not functioning properly when it relies on a property of a list

Here is a snippet of my HTML code: <tr v-for="product in products" :class="{'bg-red': product.toWrite }" :key="product.name"> <td @click="setObjectToWrite(product.name)" class="show-hover&qu ...

What methods are available for employing conditions in HTML attributes?

I need to implement a functionality in Angular where an image is loaded in a new window when the user clicks a button. The popup window will contain HTML content. Here's the Angular code snippet: <button class="btn btn-primary" type=&quo ...

Using `popWin()` in conjunction with `echo php` in Javascript

How can I create a JavaScript popup window inside an echo line? I have tried the following code but the popup window does not work: echo '<td> <a href="javascript:popWin(edit.php?id='.$row[id].')">Edit</a></td>&apos ...

Failed deployment of Node.js on Heroku results in the inability to serve static files

I have been attempting to implement two solutions I found, but they are not working as expected. The static files cannot be located. The following code is not working: process.env.PWD = process.cwd() app.set('views', path.join(process.env.PWD, ...

Tips for creating an automatic interval timer reset feature

I searched for similar questions but couldn't find any that helped me. With the assistance of some kind individuals from Stack Overflow, I was able to implement an interval timer sequence on my website. This trailer automatically displays work example ...