with Imagination: by Dustin Diaz

./with Imagination

A JavaScript, CSS, XHTML web log focusing on usability and accessibility by Dustin Diaz

Hide that sidebar

Thursday, December 22nd, 2005

A few years back I can remember visiting Sitepoint and reading an article about…something. What I distinctly remember is that I was able to hide the sidebar so that I wouldn’t be distracted by other page elements floating next to the content I was trying to focus on.

After discovering how cool that actually was, I quickly began trying to dig up how it was done but to my surprise - I came up empty handed when looking through their source.

Well, years later, my code hunting techniques have vastly improved…not to mention my code writing ;) With that said, here is a quick copy & paste job for you to play with (directly from this site)

For a quick demo of this code snippet, simply click the left icon directly above the sidebar of this page.

toggle sidebar (JavaScript)

/* used in conjuction with both the dollar $() function and addEvent */
// addEvent() found @ http://www.dustindiaz.com/rock-solid-addevent/
// $() found @ http://www.dustindiaz.com/top-ten-javascript

var togBar = {
	bar : null,
	nest : null,
	init : function() {
		this.bar = $('sideBar');
		this.nest = document.createElement('div');
		this.nest.id = 'toggler';
		this.run();
	},
	run : function() {
		$('body').appendChild(this.nest);
		this.nest.innerHTML = '<a id="open" title="Open Sidebar">Show Sidebar</a>';
		this.nest.innerHTML += '<a id="closed" title="Hide Sidebar">Hide Sidebar</a>';
		this.togglers();
	},
	togglers : function() {
		addEvent($('open'),'click',this.opener);
		addEvent($('closed'),'click',this.closer);
	},
	opener : function() {
		togBar.bar.className = 'opened';
	},
	closer : function() {
		togBar.bar.className = 'closed';
	}
};
function commonListeners() {
	togBar.init();
}
addEvent(window,'load',commonListeners);

CSS for Toggling


div#sideBar.opened { display:block; }
div#sideBar.closed { display:none; }

What it’s doing

First you’ll notice that we’re appending the open and close links to the document with JavaScript. This is mainly so that we don’t have these links just lying around on our document that when you click them - do nothing. In other words, if they make it to the page, the user most likely has JavaScript enabled.

Next you can see in the CSS we have defined two classes. ‘opened’ simply has display:block; and ‘closed’ is display:none; appropriately.

Finally, we assign an event to each link that runs the togBar.opener method, and the togBar.closer method which simply swap out the className’s. That’s it! Easy sideBar toggling with unobtrusive JavaScript

Todays question and book information

Web Standards Solutions

The winner of today’s contest will receive a copy of Web Standards Solutions by Dan Cederholm.

Give me a match on Google Fights that will make me laugh.

30 Responses to “Hide that sidebar”

  1. Derek Brown

    I got a few:

    1) Mike Tyson vs. Vincent Van Gogh

    2) Web 2.0 vs. Web 1.1

    3) Steven Hawking vs. Spock

    4) Starbucks vs. Colombian Farmer Union

  2. Jim A.

    Graceful Degradation vs Ungraceful Degradation

  3. Dante

    Jesus Christ vs Cheese and Rice

    Dustin Diaz vs Cameron Diaz

    little kids vs Michael Jackson

  4. Tony Summerville

    Cool - maybe even better if you save the hidden state in cookie so it’s remembered from page to page?

    I’ve already got that book, but here goes…

    dan cedarholm v douglas bowman

  5. Dustin Diaz

    Yea Tony,
    maybe the next version will take advantage of the cookie functions I described in my top ten list. This wouldn’t really be that difficult to do with them.

  6. Elliot Swan

    Hey, I like how you can open and close the code boxes as well…as that new, or have I just never noticed that?

    This one actually surprised me: Web Standards vs. Tables

  7. Chad Lawson

    Dating myself:

    VHS or Beta

    Crass:

    Have a Quickie or Take a Nap?

    My personal feelings are mirrored with:

    Listen to Ashley Simpson or Pick My Nose

  8. Ian Oxley

    After reading the UDASS article, I couldn’t resist:

    Yoda v. Willow

  9. Espen

    Holy crap! The hide sidebar link crashed my Firefox 1.5 :\

  10. DennisBullock.com » Blog Archive » Hide Your Side Bar

    [...] st 2005

    Hide Your Side Bar

    Dustin Diaz has a really cool tutorial on his site to allow a show/hide function for your sidebar. It’s just a little Java [...]

  11. Nate Cavanaugh

    While I think the actual function itself is pretty decent, what I’m most impressed by is your elegant OO-JS. Awesomely done :)

  12. Dustin Diaz

    These are some funny matchups. Way to go guys.

    @Espen: It doesn’t appear that anyone else had that problem. I’m sure someone would have reported it by now seeing as how I’ve had that sideBar toggle for quite some time now - and most of my user base is browsing on multiple versions Firefox.

    @Nate: Hey thanks man. I’m hoping that’s what folks are picking up with these. “Awesomely” is an awesome word ;)

  13. Dustin Diaz

    Don’t speak so soon Dustin…
    Espen, I replied before giving i a whirl just again. Then I received an email from someone else also telling me that it crashed their browser.

    Sure enough I had to try it again. Whada ya know. It crashed. Why? This is news to me. It’s been working every day until today…so thinking back on it, I didn’t upgrade firefox until last night. I’m wondering if there’s a bug in the browser. This totally used to work :o

  14. Lee

    One too many slashes in that source comment (URI) to your add event article. It’s addevent not add-event.

  15. Paul Davey

    The homeless indeed are less than the rich Dustin:
    http://www.googlefight.com/index.php?lang=en_GB&word1=homeless&word2=rich

  16. Dustin Diaz

    Alrighty, there seemed to have been an issue with Element.addClassname and Element.removeClassname. In my source I simply just used the .className property. That’s what I get for trying to take advantage of Prototype.js… now I’m wondering if it’s a bug in that.

  17. Nathan Smith

    Nice tutorial. It’s pretty cool that everything can be pre-styled, allowing for just a simple toggle. I’ve read Web Standards Solutions already, so I’m not looking to win a book here, but I just wanted to chime in with my Google Fight. This is one I often throw in Jonathan’s face, though it really has no bearing whatsoever:

    Snook vs. Smith - A Deathmatch

  18. Dustin Diaz

    Heh. That’s a good one.

    You know, I’m trying this on another computer now…and it’s still crashing firefox. I’m really concerned now…

  19. Elliot Swan

    I’m on FF 1.5 and it’s not crashing here.

  20. James Mitchell

    I am also using FF 1.5 and it isn’t crashing for me either, although it will not toggle back visible. Maybe its just me or it’s not supposed to. Either way, very cool Dustin I can think of many uses for this!

  21. John

    I’m using FF 1.5 and it crashes.

  22. Joey

    My 1.5 crashed! I’m really interested in figuring out why…

  23. welscotch

    Crashes my FF 1.5 too, and I don’t know why…

  24. Cristian Conti

    Dave Winer vs Adam Curry: c’mon Dave, kick, punch, punch, come on! Put him on the ground!

  25. David Pal

    Same here… FF 1.5 and it crashes

  26. Dustin Diaz

    Right folks. We got it. And yea, we got it. No more need to confirm that it crashes FF1.5.

    There’s something really strange about this code because it works for some - and not others. The code is flawless - and if anything, it should throw a console error.

    I’m going to play with it and see if it’s caused by any special reserved words or somethin’.

  27. Dustin Diaz

    Oh. and btw, it works totally fine after rolling back to FF1.0. Weird.

  28. Carl Camera

    Molly vs The Disney Store

    You know who we’re rooting for! :-)

  29. Umpty Dumpty

    winners vs losers

    The result:
    http://www.googlefight.com/index.php?lang=en_GB&word1=winners&word2=losers

  30. And the Winners are…

    [...] s that are in their character. 9) Hide that Sidebar Give a great match on Googlefight.com Dante http://pianoman.wordpress.com/ Dante mentioned Me vs Camer [...]

Get "JavaScript Design Patterns"

"As a web developer, you'll already know that JavaScript™ is a powerful language, allowing you to add an impressive array of dynamic functionality to otherwise static web sites. But there is more power waiting to be unlocked--JavaScript is capable of full object-oriented capabilities, and by applying OOP principles, best practices, and design patterns to your code, you can make it more powerful, more efficient, and easier to work with alone or as part of a team."

Buy JS Design Patterns from Amazon.com Buy JS Design Patterns from Apress

Submit a Prototype

All content copyright © 2003 - 2007 under the Creative Commons License. Wanna know something? Just ask.

About | Archives | Blog Search

[x] close

Loading...

Submit a prototype

By checking this prototype I agree that I am not submitting false credentials, pornography, or a hate crime website. I also understand that by submitting my entry I may or may not be accepted, and if accepted, my entry may be taken down at any given time if I violate these terms.