I’ve added a new functionality to my website, and that’s something I’ve been meaning to add since I had Spoilers. For example:
Unfortunately I don’t think I’ll be going back and editing every spoiler to insert that functionality.
I did also add it to the links on the side so I can hide a bunch of stuff by default. Enjoy!
Here’s how to do it if you’re interested:
var block = document.getElementById(blockID);
var displayProperty = block.style.display;
var text = document.getElementById(blockID + "Tog");
if (displayProperty == 'none')
block.style.display = 'block';
text.innerHTML = "Hide";
block.style.display = 'none';
text.innerHTML = "Show";
What this chunk of code does is it’ll either hide or unhide the element that it finds by the ID you pass in by checking to see if it’s currently hidden or not. If it’s hidden, it’ll unhide it. If it’s being shown, it’ll hide it. There’s also different section of code where it finds the element which has a specific ID based on the ID you passed in, which I’ll explain later. What it does is change the text to “Show” or “Hide” depending on if it’s hidden anymore.
For any element you want to hide with this toggle function, you just need to give it a UID (unique ID). For example:
<p id="toggleTest">this is a toggle test</p>
Afterwards, you’ll just need to construction a link to do the toggle. You’ll also need to give this link a UID based on the UID of the block you want to toggle. Here’s the code:
Notice how the link id is toggleTestTog and the block id is toggleTest. For my code you need to append the Tog to the UID of the block id for it to be able to know what text to change.
Here’s the above example in running:
this is a toggle test
If you want to have the item hidden by default, you just need to update code of the item to have a style attribute of display:none:
<p id="toggleTest2" style="display:none">this is a toggle test 2</p>
Here’s the corresponding link code:
Here’s the example of the code already hidden. I changed the id to toggleTest2 since it needs to be unique. I also changed the link text from Hide to Show since it’s hidden by default: