Created
October 6, 2023 08:08
-
-
Save thefloodshark/37458afcd3095276a7c356845a5024ac to your computer and use it in GitHub Desktop.
Independently Toggle Display of Elements When Clicked
This file contains hidden or 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
<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> |
This file contains hidden or 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
.content { | |
display: none; /* initially hide content */ | |
} | |
.clickable-element { | |
cursor: pointer; /* add a pointer to indicate clickability */ | |
} |
This file contains hidden or 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
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