You can create nice looking expandable regions using just HTML and some Java scripting. This is specially useful if you are to create a site without any major programming languages. Today I thought to share an expandable region that I created to give you an idea to get it done.
When the code works it will look like the following.
You should be excited by now, to get it working you need the following code inserted into a body section of a HTML file. Please note I have added the CSS also in to the page body, but if you have a separate CSS file please separate the CSS .
- <style type="text/css">
- a.headingStyle
- {
- text-decoration: none;
- margin-left: 0.5em;
- }
- a.headingStyle:hover
- {
- text-decoration: underline;
- }
- a.headingStyle span.charStyle
- {
- font-family: monospace;
- font-weight: normal;
- }
- .contentStyle
- {
- display: none;
- margin-left: 1.5em;
- margin-right: 0.5em;
- margin-bottom: 0.5em;
- border-width: thin;
- border-style: outset;
- background-color:#D2E3FF;
- }
- .upArrowStyle
- {
- height:32px;
- background-image: url('Up_Arrow_32x32.png');
- background-repeat:no-repeat;
- }
- .downArrowStyle
- {
- height:32px;
- background-image: url('Down_Arrow_32x32.png');
- background-repeat:no-repeat;
- }
- </style>
- <script type="text/javascript">
- function display(item) {
- if (document.getElementById) {
- // Retrieving the first child item.
- var firstChildItem = item.firstChild;
- // Getting the correct child item with inner HTML text.
- firstChildItem = item.firstChild.innerHTML ? item.firstChild : item.firstChild.nextSibling;
- // Toggling the + or - according to the state after click.
- firstChildItem.innerHTML = firstChildItem.innerHTML == '+' ? '-' : '+';
- // Toggling the display image after click.
- item.className = item.className == "downArrowStyle" ? "upArrowStyle" : "downArrowStyle";
- // Get the sub item to toggle visibility.
- var nextSubItem = item.parentNode.nextSibling.style ? item.parentNode.nextSibling : item.parentNode.nextSibling.nextSibling;
- // Toggle visibility of the sub item.
- nextSubItem.style.display = nextSubItem.style.display == 'block' ? 'none' : 'block';
- }
- }
- // Displaying everything if item could not be found.
- if (!document.getElementById)
- document.write('<style type="text/css"><!--\n .dspcont{display:block;}\n //--></style>');
- </script>
- <!-- If Java scripts are blocked, display everything expanded. -->
- <noscript>
- <style type="text/css">
- .dspcont
- {
- display: block;
- }
- </style>
- </noscript>
- <div>
- <!-- If + or - is required to be displayed besides the image simply change display:none; to display:block; in following sections. -->
- <h1>
- <a href="javascript:void(0)" class="downArrowStyle" onclick="display(this)">
- <span style="display:none;">+</span><span style="margin-left:34px;">Main Title 1</span>
- </a>
- </h1>
- <div class="contentStyle">Title 1 content goes here.</div>
- <h1>
- <a href="javascript:void(0)" class="downArrowStyle" onclick="display(this)">
- <span style="display:none;">+</span><span style="margin-left:34px;">Main Title 2</span>
- </a>
- </h1>
- <div class="contentStyle">
- <h2>
- <a href="javascript:void(0)" class="downArrowStyle" onclick="display(this)">
- <span style="display:none;">+</span><span style="margin-left:34px;">Sub Title 1</span></a></h2>
- <div class="contentStyle">
- Sub title 1 content goes here.</div>
- <h2>
- <a href="javascript:void(0)" class="downArrowStyle" onclick="display(this)">
- <span style="display:none;">+</span><span style="margin-left:34px;">Sub Title 2</span></a></h2>
- <div class="contentStyle">
- Sub title 2 content goes here.</div>
- </div>
- <h1>
- <a href="javascript:void(0)" class="downArrowStyle" onclick="display(this)">
- <span style="display:none;">+</span><span style="margin-left:34px;">Main Title 3</span></a></h1>
- <div class="contentStyle">Title 3 content goes here.</div>
- </div>
Remember to correct the image URLs, otherwise they will go missing.
I need to thank M. C. Matti for his great article which gave me this idea. Also you can use this method to create expandable regions in other sites which use different technologies. For example if you need to add this to a SharePoint site, just use a Content Editor web part and paste the code using Source Editor in web part properties.
Hope this helps.