Categories:

Site Search:

Find me at:


Share/Save/Bookmark

Accessible show/hide content with jquery

Update: Show/hide multiple elements independently

I'm a big fan of jquery since I don't often need to write any javascript, but for the times I do it cuts out most of the hassle of accessing on page elements, using CSS selectors and really helps with an accessible approach.

I recently wanted a section of a page to be hidden by default, with a link for users to show and hide the section. It's basically for an advanced options section of a form. What I wanted was to avoid any changes to the HTML whatsoever. Fortunately, jquery allows us to append content before an element, hide an element when the DOM is ready, and we can use an if else in order to change the link text from show to hide. Nothing groundbreaking or especially hard to do, but certainly useful. The code will hide a div with the CSS id hide_this (#hide_this), of course, alter this to suit the element you want to toggle.


// this tells jquery to run the function below once the DOM is ready
$(document).ready(function() {

// choose text for the show/hide link
var showText="Show the hidden text";
var hideText="Hide the text";

// create the toggle link
$("#hide_this").before("<p><a href='#' id='toggle_link'>"+showText+"</a>");

// hide the content
$('#hide_this').hide();

// capture clicks on the newly created link
$('a#toggle_link').click(function() {

// change the link text
if ($('a#toggle_link').text()==showText) {
$('a#toggle_link').text(hideText);
}
else {
$('a#toggle_link').text(showText);
}

// toggle the display
$('#hide_this').toggle('slow');

// return false so any link destination is not followed
return false;
				});

});

Demo of the accessible show/hide code

With javascript disabled, this text is visible by default and there is no show/hide link.

17.05.2008. 13:30

Fritz Desir said on 24.09.2008. 04:20

Question, how would you use this if you have two or more sections of text on the same page both of which requiring show/hide?

Any reply would be greatly appreciated.

Andy said on 24.09.2008. 09:10

Do you want both sections hidden simultaneously, or to have links that hide them separately?

It's easy enough to achieve both. The function could be modified slightly so that it accepts a parameter to determine which section to hide.

Roman said on 04.10.2008. 12:42

I would like to get this work with images and not text. I mean Show/Hide by clicking on a image. Is it possible?

Andy said on 07.10.2008. 15:49

Sure, just edit the "text" used for show and hide to include on/off images, e.g.

var showText="<img src='/path/to/image_on.jpg'/>";
var hideText="<img src='/path/to/image_off.jpg'/>";

Justin Young said on 12.10.2008. 00:58

Since I know nothing, could you show how to modify the function so that it accepts a parameter to determine which section to hide - if you want multiple sections hidden.

Like: section 1
section 2
section 3

Thanks! Great Site

Andy said on 13.10.2008. 20:17

Sure - I used a slightly different approach, and put it into a new article, since this is quite commonly requested functionality.

m@ said on 29.04.2009. 19:26

now...can I add a rollover effect to the image version?

Rahul - Web Guru said on 05.09.2009. 10:45

Quite cool script. Thanks mate.

ardianzzz said on 19.10.2009. 08:18

so simple, & functional too...
like it!

Kurt said on 19.10.2009. 10:57

Great code, found it very useful. I was having a bit of trouble having the same effects in IE as Firefox, however found that if you force IE8 into compatibility mode with a meta tag it worked.



Cheers.

danny said on 23.10.2009. 09:38

how would you move the hide/show to the bottom the hidden/shown material instead of above it?

danny said on 24.10.2009. 07:36

hi, just wondering if you were able to more the button below the text thats about to be hidden?

valerio said on 08.12.2009. 18:22

please notice an unpleasant css bug in IE. trying to fix that

Mark L said on 20.02.2010. 16:37

Thanks for this. Very useful. Implementing as a method to show french version of a post under wordpress.

Fabio Varesano said on 05.03.2010. 11:01

Looks like you have an unclosed p tag in before()

Simon said on 22.04.2010. 10:33

This is nice little example, thanks. I've just used something similar to show/hide a left menu in order to make room for a wide table.

Brien said on 16.07.2010. 22:14

Thanks for the example. However, it doesn't address wider accessibility issues for various disabilities. Here is another approach that might be a useful reference for your own:

http://filamentgroup.com/lab/expand_and_collapse_content_accessibly_with_progressive_enhancement_jquery/

Questions, comments, insults or praise? Have your say:

:

:

:


1 + 7 =

Articles RSS feed

Page last (manually) updated: December 23, 2009.