Skip to content
Advertisement

querySelectorAll for element contain specific class

I have few divs that contain two classes such as this:

JavaScript

where {{month.year + '-' + month.monthName + '-' + 'end'}} for a certain month is 2018-August-end

I want to select the divs that contain only 2018-August-end which I store into a variable so I tried something like this

JavaScript

but I get this error

Uncaught DOMException: Failed to execute ‘querySelectorAll’ on ‘Document’: ‘.2018-August-end’ is not a valid selector. at :1:10

why is that ?

Thanks

Advertisement

Answer

Class name dot (.) selectors can’t start with an unescaped digit (2, in your case).

The simplest solution is to just start them with a letter instead, which I strongly recommend:

Example:

JavaScript
JavaScript

Alternately, you can use the [class~=value] notation, which is functionally identical (for HTML):

JavaScript

Example:

JavaScript
JavaScript

It’s also possible to escape the first digit with . notation, but it’s painful. (You can’t just throw a backslash in front of it, as you can with some libraries like jQuery.)

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement