Skip to content

Instantly share code, notes, and snippets.

@thefloodshark
Created October 6, 2023 08:08
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 thefloodshark/37458afcd3095276a7c356845a5024ac to your computer and use it in GitHub Desktop.
Save thefloodshark/37458afcd3095276a7c356845a5024ac to your computer and use it in GitHub Desktop.
Independently Toggle Display of Elements When Clicked
<h2 class="clickable-element" onclick="toggleContent(this)">Toggle H2 1</h2>
<p class="content">Content 1</p>
<h2 class="clickable-element" onclick="toggleContent(this)">Toggle H2 2</h2>
<p class="content">Content 2</p>
<h2 class="clickable-element" onclick="toggleContent(this)">Toggle H2 3</h2>
<p class="content">Content 3</p>
.content {
display: none; /* initially hide content */
}
.clickable-element {
cursor: pointer; /* add a pointer to indicate clickability */
}
function toggleContent(element) {
var content = element.nextElementSibling;
if (content.style.display === "none" || content.style.display === "") {
content.style.display = "block";
} else {
content.style.display = "none";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment