{"id":296,"date":"2006-01-21T20:39:57","date_gmt":"2006-01-22T04:39:57","guid":{"rendered":"http:\/\/www.krunk4ever.com\/blog\/?p=296"},"modified":"2006-01-21T21:06:32","modified_gmt":"2006-01-22T05:06:32","slug":"showhide-functionality","status":"publish","type":"post","link":"https:\/\/www.krunk4ever.com\/blog\/2006\/01\/21\/showhide-functionality\/","title":{"rendered":"Show\/Hide functionality"},"content":{"rendered":"<p>I&#8217;ve added a new functionality to my website, and that&#8217;s something I&#8217;ve been meaning to add since I had Spoilers. For example:<\/p>\n<p><strong>Spoilers<\/strong>: (<a id=\"spoilers296Tog\" href=\"javascript:toggle('spoilers296')\">Show<\/a>)<\/p>\n<blockquote id=\"spoilers296\" style=\"display:none\"><p>&lt;<strong>INSERT SUPER MAJOR SPOILERS THAT YOU&#8217;D KILL YOURSELF IF YOU READ IT<\/strong>&gt;<\/p><\/blockquote>\n<p>Unfortunately I don&#8217;t think I&#8217;ll be going back and editing every spoiler to insert that functionality.<\/p>\n<p>I did also add it to the links on the side so I can hide a bunch of stuff by default. Enjoy!<\/p>\n<p>Here&#8217;s how to do it if you&#8217;re interested:<\/p>\n<p>First, you&#8217;ll need the javascript code that&#8217;ll show and hide the blocks:<\/p>\n<blockquote>\n<div align=\"left\"><code>&lt;script type=\"text\/javascript\"&gt;<br \/>\nfunction toggle(blockID)<br \/>\n{<br \/>\n&nbsp;&nbsp;var block = document.getElementById(blockID);<br \/>\n&nbsp;&nbsp;var displayProperty = block.style.display;<br \/>\n&nbsp;&nbsp;var text = document.getElementById(blockID + \"Tog\");<br \/>\n&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;if (displayProperty == 'none')<br \/>\n&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;block.style.display = 'block';<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;text.innerHTML = \"Hide\";<br \/>\n&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;else<br \/>\n&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;block.style.display = 'none';<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;text.innerHTML = \"Show\";<br \/>\n&nbsp;&nbsp;}<br \/>\n}<br \/>\n&lt;\/script&gt;<\/code><\/div>\n<\/blockquote>\n<p>What this chunk of code does is it&#8217;ll either hide or unhide the element that it finds by the ID you pass in by checking to see if it&#8217;s currently hidden or not. If it&#8217;s hidden, it&#8217;ll unhide it. If it&#8217;s being shown, it&#8217;ll hide it. There&#8217;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&#8217;ll explain later. What it does is change the text to &#8220;Show&#8221; or &#8220;Hide&#8221; depending on if it&#8217;s hidden anymore.<\/p>\n<p>For any element you want to hide with this toggle function, you just need to give it a UID (unique ID). For example:<\/p>\n<div align=\"left\"><code>&lt;p id=\"toggleTest\"&gt;this is a toggle test&lt;\/p&gt;<\/code><\/div>\n<p>Afterwards, you&#8217;ll just need to construction a link to do the toggle. You&#8217;ll also need to give this link a UID based on the UID of the block you want to toggle. Here&#8217;s the code:<\/p>\n<div align=\"left\"><code>&lt;a id=\"toggleTestTog\" href=\"javascript:toggle('toggleTest');\"&gt;Hide&lt;\/a&gt;<\/code><\/div>\n<p>Notice how the link id is <strong>toggleTestTog<\/strong> and the block id is <strong>toggleTest<\/strong>. For my code you need to append the <strong>Tog<\/strong> to the UID of the block id for it to be able to know what text to change.<\/p>\n<p>Here&#8217;s the above example in running:<\/p>\n<blockquote>\n<p id=\"toggleTest\">this is a toggle test<\/p>\n<p><a id=\"toggleTestTog\" href=\"javascript:toggle('toggleTest');\">Hide<\/a><\/p><\/blockquote>\n<p>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 <strong>display:none<\/strong>:<\/p>\n<div align=\"left\"><code>&lt;p id=\"toggleTest2\" style=\"display:none\"&gt;this is a toggle test 2&lt;\/p&gt;<\/code><\/div>\n<p>Here&#8217;s the corresponding link code:<\/p>\n<div align=\"left\"><code>&lt;a id=\"toggleTest2Tog\" href=\"javascript:toggle('toggleTest2');\"&gt;Show&lt;\/a&gt;<\/code><\/div>\n<p>Here&#8217;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&#8217;s hidden by default:<\/p>\n<blockquote>\n<p id=\"toggleTest2\" style=\"display:none\">this is a toggle test 2<\/p>\n<p><a id=\"toggleTest2Tog\" href=\"javascript:toggle('toggleTest2');\">Show<\/a><\/p><\/blockquote>\n<p>I did try to find if I knew the elementID of the object that called me, but I wasn&#8217;t able to find much info. If you know how a javascript function can figure out the object\/element that called it, please do let me know. Just leave me a short message in the comments section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve added a new functionality to my website, and that&#8217;s something I&#8217;ve been meaning to add since I had Spoilers. For example: Spoilers: (Show) &lt;INSERT SUPER MAJOR SPOILERS THAT YOU&#8217;D KILL YOURSELF IF YOU READ IT&gt; Unfortunately I don&#8217;t think I&#8217;ll be going back and editing every spoiler to insert that functionality. I did also &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.krunk4ever.com\/blog\/2006\/01\/21\/showhide-functionality\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Show\/Hide functionality&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/posts\/296"}],"collection":[{"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/comments?post=296"}],"version-history":[{"count":0,"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/posts\/296\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/media?parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/categories?post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/tags?post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}