Why doesn't querySelectorAll select all my elements?


Navin Rauniyar

In this example, querySelectorAll selects tdelements 2 to 4:

document.querySelectorAll("td + td");

<table>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>
</table>

Therefore, all tdelements greater than or equal to 2 will be selected.

But in the example below, 'div'my two elements are not selected when I use :div

document.querySelectorAll('div').style.color = 'red';

<div>foo</div>
<div>bar</div>

demo version

Why doesn't querySelectorAll work in this basic case?

mr code

querySelectorAll returns an array-like object ( NodeList ), so you can't set properties on all matched elements (like jQuery). You have to iterate through them:

var divs = document.querySelectorAll('div');

for(var i=0; i<divs.length; i++){
    divs[i].style.color = "red";
}

Note: This is different from querySelector , which always returns a single element.

You can write a helper function like:

function qsa(selector, styleProperty, value){
    var divs = document.querySelectorAll('div');

    for(var i=0; i<divs.length; i++){
        divs[i].style[styleProperty] = value;
    }
    return divs;
}

usage:

var divs = qsa('div', 'color', 'red');

Related


Why doesn't querySelectorAll select all my elements?

Navin Rauniyar In this example, querySelectorAll selects tdelements 2 to 4: document.querySelectorAll("td + td"); <table> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> </table> Therefore, all tdelements greate

Why doesn't getElementsByClassName select all my elements?

Jovan I am trying to hide multiple elements with the same class name in my webview application <div class="row"> <div class="col-md-2"> 1 </div> <div class="col-md-2"> 2 </div> <div class="col-md-2"> 3 </div> </div> I tried getElementsByClassName and querySele

How to select all elements by querySelectorAll()

Sadie Zenaloff I have a website with a catalog of products https://www.estelab.ru/cosmetics/zo-medical/ You can see in the console message after clicking the "Buy" button in the orange background ("addToCart works") But this click works for the first button. v

Why doesn't select2 set all array elements?

Andreas Hunter Here I have custom code where I am setting array to select2 value. However, when I set the array items to select2, not all items are set yet. how to solve this problem? $('#tags').select2({ multiple:true, tags: true, placeholder: 'Select t

Why doesn't select2 set all array elements?

Andreas Hunter Here I have custom code where I am setting array to select2 value. However, when I set the array items to select2, not all items are set yet. how to solve this problem? $('#tags').select2({ multiple:true, tags: true, placeholder: 'Select t

Why doesn't jQuery select all attribute elements?

Elyza Agosta I'm making labels on a website and setting up jQuery to run a function when any attribute is clicked (this is just to test the selector, not all attributes will be used in the final website). Once inside a div, no attributes are selected. Here is

Why doesn't select2 set all array elements?

Andreas Hunter Here I have custom code where I am setting array to select2 value. However, when I set the array items to select2, not all items are set yet. how to solve this problem? $('#tags').select2({ multiple:true, tags: true, placeholder: 'Select t

Why doesn't jQuery select all attribute elements?

Elyza Agosta I'm making labels on a website and setting up jQuery to run a function when any attribute is clicked (this is just to test the selector, not all attributes will be used in the final website). Once inside a div, no attributes are selected. Here is

Why doesn't QuerySelectorAll work on my modal page

Kevin I'm pretty new to coding and trying to teach myself through books and other resources available, so please don't bother me. Does anyone know how to make my close button work for all modal pages that use querySelectoror QuerySelectorAll? Currently, I'm tr

Puppeteer querySelectorAll doesn't get elements correctly

Ryan H I'm using Puppeteer and trying to use document.querySelectorAllget a list of elements and then loop and do something, however, there seems to be a problem in my code, it either returns nothing or nothing undefined, {}although my elements are on the page

QuerySelectorAll(input[type=select]) doesn't work

Kirby L. Wallace I can select all other types of elements on the page except: var elems = querySelectorAll("input[type=select]"); Once I have them, I .disabledapply in a loop: for (var i = 0; i < elems.length; i++) { elems[i].disabled = true; } This appl

QuerySelectorAll(input[type=select]) doesn't work

Kirby L. Wallace I can select all other types of elements on the page except: var elems = querySelectorAll("input[type=select]"); Once I have them, I .disabledapply in a loop: for (var i = 0; i < elems.length; i++) { elems[i].disabled = true; } This appl

Why doesn't my SQL select work?

username I keep getting this exception: Data for row/column does not exist. But the information in the selection is correct. I don't know why this always fails. string SQL = "SELECT htmlCodeBlock FROM HTML WHERE htmlID = 1"; command.CommandText = SQL; comma

Why doesn't my SQL select work?

username I keep getting this exception: Data for row/column does not exist. But the information in the selection is correct. I don't know why this always fails. string SQL = "SELECT htmlCodeBlock FROM HTML WHERE htmlID = 1"; command.CommandText = SQL; comma

Why doesn't .then() execute at all for my Promise?

AlphaHowl I have a chain resolveof promises that I want to run, promises will appear when done (see code below for more context), and want my .then()block of code to run...but it doesn't. Here is my code: function notifications_for_each_in_array(iteration, th

Why doesn't .then() execute at all for my Promise?

AlphaHowl I have a chain resolveof promises to run, promises will appear when complete (see code below for more context), and want my code .then()block to run...but it doesn't. Here is my code: function notifications_for_each_in_array(iteration, the_array, er

Why doesn't my program remove "all"?

Sankov My problem is that when I output this code, it doesn't output what I want to remove "all". It outputs exactly what the first print statement does. Here is my code: // RemoveAll // Spec: To remove the "all" // ArrayList remove() exercise import java.uti

Why doesn't .then() execute at all for my Promise?

AlphaHowl I have a chain resolveof promises that I want to run, promises will appear when done (see code below for more context), and want my .then()block of code to run...but it doesn't. Here is my code: function notifications_for_each_in_array(iteration, th

Why doesn't .then() execute at all for my Promise?

AlphaHowl I have a chain resolveof promises to run, promises will appear when complete (see code below for more context), and want my code .then()block to run...but it doesn't. Here is my code: function notifications_for_each_in_array(iteration, the_array, er

Why doesn't .then() execute at all for my Promise?

AlphaHowl I have a chain resolveof promises to run, promises will appear when complete (see code below for more context), and want my code .then()block to run...but it doesn't. Here is my code: function notifications_for_each_in_array(iteration, the_array, er

Why doesn't .then() execute at all for my Promise?

AlphaHowl I have a chain resolveof promises to run, promises will appear when complete (see code below for more context), and want my code .then()block to run...but it doesn't. Here is my code: function notifications_for_each_in_array(iteration, the_array, er

Why doesn't my program remove "all"?

Sankov My problem is that when I output this code, it doesn't output what I want to remove "all". It outputs exactly what the first print statement does. Here is my code: // RemoveAll // Spec: To remove the "all" // ArrayList remove() exercise import java.uti