Skip to content

Instantly share code, notes, and snippets.

@cballenar
Last active December 11, 2022 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cballenar/89be83d0625877ed9fee2a6b337e71eb to your computer and use it in GitHub Desktop.
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
/**
* 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