My current issue revolves around a button that is designed to open or close a collapsible div
. The HTML structure of this element looks like the following:
<div class='outer-collapsible'>
<button type='button' class='collapsible'>Targets and Victims</button>
<div class='content'>
<p>Lorem ipsum...</p>
</div>
</div>
While attempting to target all button
elements with the collapsible
class, I am utilizing nextElementSibling
to select the <div class='content'>
as shown below:
let collapsibles = document.querySelectorAll('.collapsible');
for (let i = 0; i < collapsibles.length; i++) {
if (collapsibles[i].nextElementSibling.style.display == 'block') {
// perform certain actions
}
}
However, it appears that even though nextElementSibling
successfully selects the correct div
, it does not recognize any style attributes specified in my CSS file. Does nextElementSibling
solely identify inline styles? While I could explore alternative methods to accomplish this task, I am curious as to why nextElementSibling
correctly identifies the element but fails to detect the styling accurately?
EDIT:
CSS
.collapsible {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}
.active,
.collapsible:hover {
background-color: #ccc;
}
.content {
padding: 0 18px;
display: none;
overflow: hidden;
background-color: #f1fff8;
}