I'm trying to find in my DOM all the img elements with 2x class using Vanilla JS. I'm using the querySelectorAll method like this:
document.querySelectorAll('img.2x');
But it throws this error at the console log:
Uncaught DOMException: Failed to execute 'querySelectorAll' on 'Document':
'img.2x' is not a valid selector.
Why img.2x is not a valid selector? Thanks.
While it certainly does look valid, you'll need to explicitly escape any digits that begin a CSS class in order to use it within your selector:
document.querySelectorAll('img.\\2x');
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With