Why is this not a valid CSS selector using querySelectorAll in JS?

I am trying to find in my DOM all elements imgwith a class 2xusing Vanilla JS. I use the method querySelectorAllas follows:

document.querySelectorAll('img.2x');

But it throws this error in the console log:

Uncaught DOMException: Failed to execute 'querySelectorAll' on 'Document':
'img.2x' is not a valid selector.

Why is img.2xnot a valid selector? Thanks.

+6
source share
1 answer

Despite the fact that it certainly looks really valid, you need to explicitly avoid any digits starting with the CSS class in order to use it in your selector:

document.querySelectorAll('img.\\2x');
+6
source

Source: https://habr.com/ru/post/1015948/


All Articles