This message appear at the log of Chrome.
I’ve tried to use onclick=
property on button
tags, but continue with the same error
My code:
window.addEventListener("load", function () {
const act = document.getElementsByClassName("collapsible-btn");
for (i = 0; i < act.length; i++) {
act[i].addEventListener("click", function () {
const allPanels = document.querySelectorAll(".history-panel");
allPanels.forEach((panel) => {
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
}
});
const allButtons = document.querySelectorAll(".collapsible-btn");
allButtons.forEach((button) => {button.classList.remove("active");
});
let panel = this.nextElementSibling;
let content = panel.nextElementSibling;
this.classList.toggle("active");
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = content.scrollHeight + "px";
}
});
}
});
2
Answers
Please delegate and test there IS a nextElementSibling
Alternatively and safer would be to use querySelector from the buttons closest parent container
I think is triggered when you are trying to access the scrollHeight property of an element that is null.
The culprit seems to be in these lines:
let panel = this.nextElementSibling;
let content = panel.nextElementSibling;
To fix the error, you should try add null checks: