<ul class="my-class">
<li>First</li>
<li><a>Second</a></li>
<li class="find-siblings">All of my siblings turned red, <em> but my child</em><strong>ren didn't!</strong></li>
<li><a><span>Third</span></a></li>
<li><a><span>Fourth</span></a></li>
</ul>
<ul class="my-class">
<li><a><span>Fifth</span></a></li>
<li><a><span>Sixth</span> <em>Sixth</em> <span >Sixth</span></a></li>
<li class="find-siblings"><a>Seventh</a></li>
<li><a>Eighth</a></li>
</ul>
html {
background: #fbdf00;
font-size: 20px;
}
// selects only the first '.find-siblings' element
let item = document.querySelector('.find-siblings');
// :scope is necessary for preventing the child elements of 'item' from being selected
// see what happens to the text when you remove ':scope >'
let siblings = item.parentNode.querySelectorAll(':scope > :not(.find-siblings)');
// loop through the siblings
for (let i = 0; i < siblings.length; i++) {
siblings[i].style.color = 'red';
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.