Bug in Javascript with your ‘details elements’ helper
-
Your codeblock here, has a slight bug
// from https://stackoverflow.com/a/70062967/5317678 // open closed details elements for printing window.addEventListener('beforeprint',() => { const allDetails = document.body.querySelectorAll('details'); for(let i=0; i<allDetails.length; i++) { if(allDetails[i].open) { allDetails[i].dataset.open = '1'; } else { allDetails[i].setAttribute('open', ''); } } }); // after printing close details elements not opened before window.addEventListener('afterprint',() => { const allDetails = document.body.querySelectorAll('details'); for(let i=0; i<allDetails.length; i++) { if(allDetails[i].dataset.open) { allDetails[i].dataset.open = ''; } else { allDetails[i].removeAttribute('open'); } } });
It should look like
window.addEventListener('beforeprint',() => { const allDetails = document.body.querySelectorAll('details'); for(let i=0; i<allDetails.length; i++) { if(allDetails[i].open) { allDetails[i].dataset.open = '1'; } else { allDetails[i].setAttribute('open', ''); } } }); // after printing close details elements not opened before window.addEventListener('afterprint',() => { const allDetails = document.body.querySelectorAll('details'); for(let i=0; i<allDetails.length; i++) { if(allDetails[i].dataset.open) { allDetails[i].dataset.open = ''; } else { allDetails[i].removeAttribute('open'); } } });
In the process of smashing the first window.addEventListener into one line, you inadvertently included the *second* window.addEventListener into the comment preceding it
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Bug in Javascript with your ‘details elements’ helper’ is closed to new replies.