Last active
December 11, 2022 12:16
-
-
Save cballenar/89be83d0625877ed9fee2a6b337e71eb to your computer and use it in GitHub Desktop.
Toggle Summary Message. Changes the content of a details' summary to indicate whether clicking it will open or close it. Assumes the <summary> lies directly under the <details> tag and contains `data-message-open` and `data-message-close` attributes with the respective values for each case. View demo: https://codepen.io/cballenar/pen/PoagMxz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Toggle Summary Message. | |
* Changes the content of a details' summary to indicate whether clicking it | |
* will open or close it. Assumes the <summary> lies directly under the | |
* <details> tag and contains `data-message-open` and `data-message-close` | |
* attributes with the respective values for each case. | |
* | |
* @param {Event} event | |
*/ | |
function toggleSummaryMessage(event) { | |
const summary = event.target; | |
summary.innerText = summary.parentElement.open ? summary.dataset.messageOpen : summary.dataset.messageClose; | |
} | |
// call for all summaries | |
const summaries = document.querySelectorAll('details summary'); | |
summaries.forEach((detail)=>detail.addEventListener('click',toggleSummaryMessage)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment