Within the DOM element #installations, there are multiple children elements, with only one having a class of .selected. My goal is to select this element along with the first 3 remaining elements that do not have the class .selected in order to display only 4 elements total.
The challenge lies in the following expression:
#installations > *:not(.selected):nth-of-type(-n+3), .selected
The issue is that :nth-of-type() ignores the :not() selector and simply selects the first 3 children of #installation. For example, if the HTML looks like this:
<div id="installations">
<div id="one"/>
<div id="two"/>
<div id="three" class="selected"/>
<div id="four"/>
<div id="five"/>
</div>
Only "one", "two", and "four" will be selected, excluding "three". This happens because :nth-of-type() has limited options due to the exclusion of the selected element by :not(), resulting in (one, two, four) being chosen. The later part of the selector , .selected
then includes the selected element.
If the .selected element is not among the first four elements but instead, let's say, the sixth element, the selection would consist of the first three elements plus the sixth.
To clarify: it is acceptable to select the .selected element along with 3 adjacent elements. However, this also poses difficulties when the .selected element is within the last 3 elements as selecting the next 3 becomes problematic.