I'm sure I am missing something really easy here but I am just trying to add a click event to one of my elements but the console keeps stating 'Null is not an object'
Here is my markup
<section class="section active">
<div class="hidden-content">
</div>
<button class="button"></button>
</section>
<section class="section">
<div class="hidden-content">
</div>
<button class="button"></button>
</section>
<section class="section">
<div class="hidden-content">
</div>
<button class="button"></button>
</section>
The javascript I am using
document.querySelector('.section.active .button').addEventListener('click', function(){
document.querySelector('.section.active .hidden-content').classList.add('show')
});
I know I could do a queryselectorAll like this and add the click handler
var hiddenContent = document.querySelectorAll('.button');
for (var i = 0; i < button.length; i++) {
button[i].addEventListener('click', function(){
document.querySelector('.section.active .hidden-content').classList.add('show')
});
}
The reason I'm asking is I know that there would only ever be one (section.active .button) at any one time which is why i was surprised queryselector wouldn't work for me as it would only return the first one it found which would be this. (Unless I am wrong of course)
Appreciate any advice to where I have gone wrong.
Thanks