What is the best way to obtain various classed elements within a single node list?

I'm trying to select two different elements with different classes within the same node list. For example, if I could use this code:

document.querySelectorAll(".a, .b");

It would mean selecting elements with the ".a" class, but also including any elements with the ".b" class that are in between them in the list, not just at the end. They should be placed according to their position in the structure as if they all had the same class.

For instance, if I have the following HTML:

<div class="a">
<div class="b">
<div class="a">

The first element with the ".a" class would be listed first, followed by the ".b" element, and then the second element with ".a" class would be third in the list.

Answer №1

Perhaps I may not fully grasp your inquiry, but it appears that the solution you have should suffice:

const elements = document.querySelectorAll(".x, .y");

console.log(elements);
for (let j = 0; j < elements.length; j++) {
    console.log(elements[j]);
}

http://jsfiddle.net/z8pcfs1j/2/

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

"Typescript React encounters issues with recognizing props handed over to a functional component, resulting in error code ts 274

Currently, I am engaged in a react project that involves typescript. The goal is to exhibit movie character details obtained from an API through individual cards for each character. This should be achieved by utilizing react functional components along wi ...

The gap persists even when overflow is used in CSS

My HTML table is set up with an overflow property to ensure it doesn't exceed a specific height, but the following div is not being positioned directly below the scrolling table as intended. Even though I am using overflow: hidden, I can still select ...

Disabling the default validation message in HTML form inputs

Is there a way to change the default message that appears when submitting an empty HTML text input? I would like the box border to turn red instead. Can anyone help me achieve this? Below is the code for the text input box: <div class="input-group ...

As you scroll, the header gradually reduces in size, but at the point where the reduction occurs, a slight flick

As I develop a website, I have implemented a sticky header that shrinks after scrolling past the window height. I experimented with two versions - one using Vanilla-JS and another with jQuery. Both versions work fine, but the issue arises when the header s ...

Javascript prototype issue - Variable stays undefined

Check out the following code snippet: function Person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; } var person=new Person("MyName","MySirname",14,"B ...

Every time I attempt to publish a new page on my website, it doesn't load all at once. Instead, it loads in

Whenever I try to upload a page on my website, it doesn't load properly. Instead of loading all at once, it loads in patches. //$("#idUpcmgMtch").append(nwhtml); //var slyObj = scrlr(); slyObj.reload(); var actid=""; if(pagename="create-team"){$ ...

The password prompt window refuses to align with the center of the screen. This

I've been struggling to position the Javascript popup notification using the width, height, top, and left parameters in the window.open function. No matter what I attempt, it stubbornly remains in the top-left corner. Can someone offer some guidance o ...

When the collapse button is clicked, I aim to increase the top value by 100. Subsequently, upon a second click, the top value should

https://i.stack.imgur.com/p9owU.jpghttps://i.stack.imgur.com/Kwtnh.jpg I attempted to utilize jQuery toggle, but unfortunately, it is not functioning as expected. <script> $(document).ready(function () { $(".sidebar ul li&quo ...

WebStorm alerts users with a warning about an incomplete statement in JavaScript code style

Currently, I am utilizing WebStorm 2016.1 for a NodeJS project and encountering difficulties with code styling issues that are triggering warnings. One particular issue involves gray blocks that appear when a line is not terminated with a semi-colon. Howe ...

CSS with a "true" space for a fixed element (ensuring the previous element does not overlap with the current fixed element)

I am facing an issue with the layout of my webpage. I have two horizontal columns - the left one is fixed, along with a bottom footer that is below the second content column. When I add a border to the content column, it extends all the way down to the fo ...

The transition from Vuetify3's VSimpleTable to VTable is ineffective and unsuccessful

The v-simple-table component has been renamed to v-table Old code that was using v-simple-table may not work correctly after the renaming. It is recommended to use v-data-table with the same data, as it works correctly. https://i.sstatic.net/3GVdYMWl.png ...

Modify the color of the footer area and eliminate any underlines present

In the footer section, the tags are displaying in blue color and I would like to remove the line under the li tags. If necessary, I can provide additional HTML or CSS code for reference. Below is the snippet of HTML and CSS related to the footer: My HTML ...

Apply the :after pseudo-class to the specified element

I am struggling with implementing an overlay effect on two images on my website. My goal is to add an overlay when the images are clicked, and I have tried creating this effect using the after pseudo class. Unfortunately, the overlay does not seem to wor ...

The medium breakpoint is utilized in Bootstrap 4 for printing purposes

Recently, I made updates to some pages to enhance their mobile friendliness. However, we encountered an issue where some users are printing the pages using tablet styles instead of desktop styles, regardless of whether they are in portrait or landscape mod ...

What is the method for printing a webpage that has been modified using JavaScript?

Take a look at this screenshot to see what I'm working with. Users have the ability to modify the page by removing items, adding new items, or marking items as complete by crossing them out. I want users to be able to print the altered page using Jav ...

Tips for centering Font Awesome icons in your design:

I'm struggling to align the font awesome icons with a lower text section in the center. Here is the HTML code snippet: .education-title { font-family: Alef, Arial, Helvetica, sans-serif; text-align: center; font-size: 1.5em; } .edu-co ...

What steps are necessary to add a Contact Us form to my HTML website?

Currently, I am searching for a way to add a "Contact Us" section to my website that is completely HTML-based. I believe the best approach would involve using a PHP script to handle sending emails from a form on the Contact Us page, but I am not familiar ...

Modify the code to interpret a new JSON structure

I have a piece of code that is designed to read JSON data: $.getJSON("data.json", function(data){ var output = ''; $.each(data, function(index, value){ output += '<li>' + value.title + '</li>'; } ...

Error: The function `push` cannot be used on the variable `result` (TypeError)

Here is a snippet from my react component const mockFetch = () => Promise.resolve({ json: () => new Promise((resolve) => setTimeout(() => resolve({ student1: { studentName: 'student1' }, student2: { studen ...

Generic partial application fails type checking when passing a varargs function argument

Here is a combinator I've developed that converts a function with multiple arguments into one that can be partially applied: type Tuple = any[]; const partial = <A extends Tuple, B extends Tuple, C> (f: (...args: (A & B)[]) => C, ...a ...