Currently, I am working on a WordPress website that utilizes a Table of Contents plugin. The plugin simply identifies headings and adds them to the table of contents.
I am aiming to achieve a specific functionality on my website (). When the window width is at or below 768px, I want to trigger a "click" event on the [ Collapse ] anchor link to hide the contents upon loading on mobile, and only expand them when loaded on desktop pixels (1280px, etc).
function tocToggle2() {
if (window.innerWidth <= 768) {
document.getElementsByClassName('lwptoc_toggle_label').click();
}
}
window.onload = tocToggle2;
I would appreciate any insights or guidance on the correct code for this. My expertise lies in building websites on Elementor and my knowledge of JavaScript is basic.
I have attempted various approaches based on my research and browsing through Stack Overflow without success.
For adding CSS and JS codes into my WordPress website, I use the Custom CSS & JS plugin, so please refrain from suggesting JQuery solutions.
EDIT: Made some corrections to the provided codes.