I have this code for collapsible items.
var coll = document.getElementsByClassName("collapsible");
for (var i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var p = this.nextElementSibling;
if (p.style.maxHeight){
p.style.maxHeight = null;
} else {
p.style.maxHeight = p.scrollHeight + "px";
}
});
if (window.screen.availWidth >= 768) coll[i].onclick.apply(coll[i]);
}
The last line is meant to automatically expand all collapsibles on larger screens. But it gives me an error:
Uncaught TypeError: Cannot read property 'apply' of null
How can I fix this?