Having trouble targeting multiple CSS classes with JavaScript? querySelectorAll doesn't seem to be doing the trick. How can

After creating a navigation and header section that change color along with the hero text as the user scrolls down the page, I am facing an issue. I have provided a link to the working example on JSFiddle:

https://jsfiddle.net/mfen723/9swhejo5/35/

The problem is that I need to select all three icon bars used for the navbar toggler so they can also change color in sync with the nav, hero background, and text. However, I am unable to select all 3 icon bars simultaneously.

Currently, I am using the following function which utilizes document.querySelector. However, this method only selects the first matching element:

const heroIconBarScroll = document.querySelector('.icon-bar.bar-top', '.icon-bar-bar-middle', '.icon.bar-bottom');

window.addEventListener('scroll', () => {
    if (window.scrollY >= 46) {
        heroIconBarScroll.classList.add('hero-icon-bar-scroll');
    } else if (window.scrollY < 56)
        heroIconBarScroll.classList.remove('hero-icon-bar-scroll');
});

I tried using document.querySelectorAll instead, but it does not seem to select any of the icon bars. Any advice or suggestions would be greatly appreciated.

Answer №1

Instead of passing multiple arguments, pass a single selector string with commas.

const elements = document.querySelectorAll('.icon-bar.bar-top, .icon-bar.bar-middle, .icon-bar.bar-bottom');

If you want to select all matching elements instead of just the first one, use document.querySelectorAll rather than document.querySelector.

You will then need to iterate over the elements in the NodeList to make changes to each one individually.

elements.forEach(element => element.classList.add('hero-icon-bar-scroll'));

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

Struggling to insert a high-quality image into inline divs of exactly 33.3333% width

After creating 3 inline divs, each taking up 33.333% of their parent width, I encountered an issue while trying to insert images into them. Whenever the image exceeds the 33.333% width of the div, it overflows outside the container. My goal is to make the ...

What causes the "v-col" width to suddenly alter after adding the "bg-red rounded" class?

I'm struggling to organize the data into blocks using Vuetify. When I apply the "bg-red rounded" class to the "v-col", it alters the width of the column, resulting in an undesired grid structure. Here is the template section of my Vue file. Some data ...

Using Vue to bind a class attribute with multiple Tailwind classes

I am attempting to associate an attribute and multiple tailwind classes with an HTML element. This is specifically for a tab menu where the goal is to dynamically display the title of the "active" tab and apply additional tailwind classes to modify the app ...

Encountered an error: Unable to access property 'tween' of an undefined variable in the Masterslider.js script

My webpage features three sliders using Master Slider alongside Fullpage.js. var slider = new MasterSlider(); slider.setup('masterslider', { width: 300, height: 300, });   slider.control('lightbox'); slider.control('slidei ...

What is the best way to customize the color of selected items in a RadComboBox using CSS?

I'm curious if it's possible to modify the "background-color" value for the selected items in a radCombobox. Below is the CSS code I've been using: (Despite being able to change other elements, I can't seem to alter the color of highli ...

Initiate CSS3 animations on each individual slide within the slider

I have integrated flex slider into my project. There are 3 CSS3 animations implemented, where the animations work perfectly on the first slide when the website loads. However, upon switching to the second slide, the animations do not start. I am seeking ...

How can I add a blank selection at the bottom of my ng-options dropdown?

At the moment, my setup looks something like this (in a simplified form): <select ng-model=model.policyHolder ng-options="person.index as person.name for person in model.insurance.persons"> <option value>Someone else </select> This co ...

Optimal Approach for Implementing a Loading Animation in React JS

While attempting to replicate my local project in Code Sandbox, I stumbled upon an error. My goal in these projects is to display a loader spinner while fetching data from an Express + GraphQL server, but unfortunately, the spinner doesn't hide once t ...

Is there a method to bypass the need for app.get() for each static file in express?

As I delve into using express.js, I'm faced with the task of serving numerous static files from my server - whether it's a .css, .jpg, .svg, .js, or any other file type. Is there a way to accomplish this without having to manually use app.get() f ...

Error logging into PHP and MySQL

As a newcomer, I'm encountering some issues with my code. Whenever I run it, an error message pops up saying, "Invalid login information. Please return to the previous page. Database not selected." However, upon reviewing my code thoroughly, everythin ...

The suffix input-group-addon in Bootstrap 3 is not properly connected to the form field

Utilizing Bootstrap 3 within a C# ASP.NET MVC5 project. I've encountered a peculiar issue while attempting to use input-group-addon to append a 'GB' suffix to a form field. Here is the C# code snippet: @using (Html.BeginForm()) { @Htm ...

Prevent Vertical Scrolling with Jquery on right-click functionality

When a user right-clicks in my application, I present them with some options. However, when the user clicks on an option such as "copy," the page always scrolls to the top. I am struggling to prevent this automatic scrolling. I attempted to use the anima ...

Implementing sliders for interactive functionality with an HTML table

I need to implement sorting and sliding features on an HTML table using jQuery. Sorting has already been achieved by utilizing the DataTable.js jQuery library. The challenge now is to incorporate a slider into the table, with all subject columns being scro ...

An effective method for creating responsive layouts for custom-shaped HTML elements in a board game

Can anyone guide me on aligning spaces responsively on a board in HTML using Angular 1.x? I've tried using vw/vh's and %s, but consistency breaks on different view sizes. Any straightforward suggestions to address this issue? Avoiding multiple c ...

Skybox in Three.js failing to load

I'm brand new to working with Three.JS and I'm struggling to figure out how to add a skybox to my scene. Despite not encountering any errors in my code, I can't seem to get it to work which is quite frustrating. Any guidance or assistance on ...

Scrollbar is always visible on Angular Flex Layout

As someone who is just starting to work with Angular and flex layout, I have a simple page setup consisting of a header with a side navbar and router outlet. I encountered an issue where my page always displays a scrollbar. If I remove fxFlexFill, the sc ...

Troubleshooting Problem with Scrolling to the Bottom of a DIV Using J

I've been experimenting with creating a chat box in jQuery that automatically scrolls to the bottom when the page loads. I've tried various methods, but most of them haven't worked as expected. One method I found animates a scroll down, but ...

What is the best way to fill the inside of a 2D circle using THREEjs?

Below is the code that generates a circle with a blue outline on a dark background: let camera = new THREE.OrthographicCamera( -1.0, 1.0, 1.0, -1.0, -1.0, 1.0 ); var radius = 1; var vertices = []; for(let i = 0; i <= 360; i++){ vertices.push(new TH ...

Determine the appropriate text color to use according to the background color

I am looking for a CSS or SASS solution to apply red text color to paragraphs with white or transparent backgrounds, and yellow text color to paragraphs with non-white backgrounds. Here is an example of what I'm trying to achieve: p { @if $selectedP ...

Expandable Grid Sections in React MUI

Is there a way to create a grid layout where items with showDefault: true are always displayed at the top, and then users can click an arrow button to expand the grid and also show the items with showDefault: false? Any suggestions on how to achieve this? ...