The problem you have is in your requirement:
I need to check if all divs with the class of listItem also have the class of inactive to give the errorItem class the style of display:block
While we can set a style for the final <div>
element based on its preceding siblings, we can't (without knowing how many there might be) 'check if all divs' have the inactive
class. We can, however, use the sibling combinator (+
) and a cumbersome selector:
.errorItem {
display: none;
}
.listItem.inactive + .listItem.inactive + .listItem.inactive + .listItem.inactive + errorItem {
display: block;
}
This is, however, ridiculous (especially if there's a dynamic number of elements preceding the .errorItem
element.
If there's a class-name applied for an element which does match the supplied filters, active
for example, this is much simpler, and achieved by:
.errorItem {
display: block;
}
.listItem.active ~ .errorItem {
display: none;
}
Also, as pointed out in the comments, the negation operator is also available (though, obviously, it depends on implementation by the browser in use), which would lend itself to the selector:
.errorItem {
display: block;
}
.listItem:not(.inactive) ~ .errorItem {
display: none;
}
On the whole, I'd strongly suggest using JavaScript to support this functionality, especially since the use of Wookmark implies JavaScript (if not necessarily jQuery) use in the same site already.
Native JavaScript:
function hasPrecedingSibling (elem, state) {
if (!state) {
return false;
}
var found = false,
cur = elem;
while (cur.previousElementSibling && found === false) {
if (cur.classList.contains(state)) {
found = true;
}
else {
cur = cur.previousElementSibling;
}
}
return found;
}
[].forEach.call(document.querySelectorAll('.errorItem'), function (err) {
err.style.display = hasPrecedingSibling (err, 'active') ? 'none' : 'block';
});