I want to select an element using querySelector(...)
Let us assume to have a html-fragment like:
<div class="Class1_a123 Class2_z987">Div1 content</div>
<div class="Class1_a123">Div2 content</div>
I want to select only the first div, if it (and this is set dynamically) has the second class. So I wrote the selector as follows:
document.querySelector("div[class^='Class2']");
The result: Nothing is selected. So I wonder, whether there is a hierarchy, which class stands in front of the class list. I thought it checks for all contained classes, whether they start with the expression I need.
Is there a fundamental mistake in my mind and/or understanding, how selectors are working?
If this is right, that the order of classes inside an elements classlist can be important for the layouting/styles of the web-site?
In this case I have to think about all code I've written in past, where I used the class^='...'
selectors and have it to replace by class*='...'
to let it work as expected.
Any suggestions/corrections are welcome.