I've come up with an alternate solution done entirely through javascript. The widget templates are not touched and adding collapse functionality to multiple widgets is much easier. All it takes is adding my script and the ID of the widget you want to make collapsible. Copy and paste the below script into your template just above the </body> tag.
<script type='text/javascript'>How it Works
//<![CDATA[
function addShowHide(strWidget) {
var objWidget = document.getElementById(strWidget),
objHeader = objWidget.getElementsByTagName("h2")[0];
if(typeof(objHeader) != 'undefined')
{
var strContWidget = "'cont" + strWidget + "'";
var objCont = objWidget.getElementsByTagName("div")[0];
if(objCont.id == ""){objCont.id = "cont" + strWidget;}
else { strContWidget = "'" + objCont.id + "'"; }
var strASH = '<a title="Show or Hide" href="javascript:doShowHide(' + strContWidget + ')"> +/- </a>';
objHeader.innerHTML = strASH + objHeader.innerHTML;
}
}
function doShowHide(strCont) {
var objCont = document.getElementById(strCont);
if(objCont.style.display == 'none') { objCont.style.display = 'inline';}
else { objCont.style.display = 'none'; }
}
//]]>
</script>
There are two functions in the script. The first adds a show/hide link in front of the widget title set to show/hide the first <div> element in the widget. When the link is clicked, the second function toggles the widgets content display style. Adding the functionality is a simple mater of finding the widget ID, then calling the first function.
Setting up your First Widget
Finding a widget ID is easiest done by loading your blog, right clicking the page and viewing source. From there hit Ctrl+F and type in the widget title. For instance to make the Blog Archive collapsible search for Blog Archive and you'll find <div class='widget BlogArchive' id='BlogArchive1'> nearby. Now we need to set the widget with ID BlogArchive1 to show/hide by adding addShowHide("BlogArchive1"); to the script just before the //]]> at the end. Preview the changes and look for a +/- link just in front of the Blog Archive title. If it's there, Save Template and view the blog. Click on the link and the archive should instantly collapse, then expand back out on your next click. Simple!
Conclusion
My script makes adding show/hide functionality to your widgets simple and easily scalable to any number of widgets. Perform the same steps in the Blog Archive example for each widget you'd like to work this way. It's certainly much simpler than the alternative solution. Give it a try for yourself!
P.S.: I have an idea for another function to automatically add show/hide to all sidebar widgets. I may include it in a future posting if I find there's interest.
14 comments :
Hi, it works great.
But at the moment, my list is shown. Only when I click the +/- it becomes hidden. How do I keep it hidden from the start and the list expands only when I click it?
Your help greatly appreciated.
There are a couple of options to start from a hidden state. First, you could add a CSS rule to initialize a widget as hidden like this #BlogArchive1 .widget-content { display: none; }
Each hidden widget would need the same style, which is probably the easiest approach.
Second option, modify my script to accept a second parameter to set the initial state. Change line 3 to this:
function addShowHide(strWidget, bHide) {
Add this line after the first else line:
if(bHide) { doShowHide(objCont.id)}
Finally, change the add commands to look like:
addShowHide("BlogArchive1", true);
Option 1 looks cleaner in my opinion, so I'd probably use CSS rules.
Thanks I tried Option 1 and it works perfectly. Thank you. Muaks!
Oh, goodness, this is EXACTLY what I have been looking for. Until now I've used one that seemed to... loop over all the labels and re-list them itself? All I wanted to do was hide the widget container itself so I could keep the gadget options [show post count, cloud, etc]. This JS does exactly that, and it does it well. AND it can be used on any widget!
I used approach #2 from your comment above to pre-collapse the widget, because I like to keep my CSS as simple as possible [since there's so much of it...]
I can't express how pleased I am with this. :D
Nanoinfinity, Glad you like it thanks for stopping by. I may just have to update this script with a little more stylish collapse graphic someday. :)
Fantastic...been searching for this all night.
I am such a noob when it comes to the HTML/Java stuff so this was great.
Let us know if you do decide to update the graphic :)
Good stuff! Thanks!
Is it possible to put a picture instead of the +/- ?
I'm kinda new at this, but I don't think <img src= would work.. or would it?
Yes, an <img> tag in place of +/- in the source is possible. I have not designed a graphic for one though. It's somewhat a personal style and color scheme specific decision. I can throw together an example with a graphic that changes on clicks for others to customize I suppose.
Here's a fairly dirty graphical version using JQuery. It's quite rough and packaged in files I'm hosting through google code. You can override the styles in the stylesheet to whatever you like. Still debating whether to make a new post out of this or not, but here's an example implementation off my test blog. Put this in the <head> section of your template.
<script src='http://code.jquery.com/jquery-latest.js'/>
<script src='http://jsblogstop-blogger-feed-tools.googlecode.com/svn/widgetTools/showHideWidgets.js'/>
<link href='http://jsblogstop-blogger-feed-tools.googlecode.com/svn/widgetTools/showHide.css' rel='stylesheet' type='text/css'/>
<script type='text/javascript'>
//<![CDATA[
$(document).ready(setShowHide);
function setShowHide() {
addShowHide("BlogArchive1", "light");
addShowHide("HTML4", "dark");
}
//]]>
</script>
Change the addShowHide lines to whatever is appropriate for your blog. The second parameter sets the color scheme. Use "light" for a black button with white arrow, for use on white style backgrounds. Use "dark" for a slightly grey button with black arrow. The visual look is far from stunning, but the code and css framework for something better is there.
Hi, This is not working for my blog
My blog : http://www.cookingspace.blogspot.com/
e.g
+/-vegetable delight (this is label)
eggplant (label)
cabbage (label)
carrot (label) these are showing even after i added widget +/-
I want sub elements should be visible after clicked on +/-
Is this possible
I have aproblem with my multitab colapseable :
It doesn't work.
Here is it: http://www.petropedia.blogspot.com
2nd option works fine in my template...thanks alot..keep it up
Hi!
Like most of the people who have commented here, even I was looking for something like this to keep my blog clean (and add lots of content in sidebar :D) THANK YOU SO MUCH!!
As per your instructions in the comment section, I have modified the code and added CSS for the widgets to be collapsed by default.
It did work when I had two or three widgets but now it does not seem to work. I checked the code again and I think I have done everything right ..
Would you please take a look at http://www.artihonrao.net and help me figure out what must have gone wrong? I can email you the blog template if "view source" does not help..
Regards
Arti Honrao
I just found this, and I want to thank you for providing my with the perfect solution!
Post a Comment