Skip to content

Instantly share code, notes, and snippets.

@michaelbragg
Created April 16, 2013 14:53
Show Gist options
  • Save michaelbragg/5396589 to your computer and use it in GitHub Desktop.
Save michaelbragg/5396589 to your computer and use it in GitHub Desktop.
jQuery Toggle
/*
// Hide only visually, but have it available for screenreaders: h5bp.com/v
*/
.visuallyhidden {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
/*
// Extends the .visuallyhidden class to allow the element to be focusable
// when navigated to via the keyboard: h5bp.com/p
*/
.visuallyhidden.focusable:active,
.visuallyhidden.focusable:focus {
clip: auto;
height: auto;
margin: 0;
overflow: visible;
position: static;
width: auto;
}
$('.js-toggle__button').click(function() {
$(this).parents('.toggle').find('.js-toggle__hidden').toggleClass('visuallyhidden');
});
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>jQuery Toggle</title>
</head>
<body>
<div class="toggle">
<article>
<p>Text to be shown 01</p>
<button class="js-toggle__button">Show 01</button>
</article>
<article class="visuallyhidden js-toggle__hidden">
<p>Text to toggle 01</p>
<button class="js-toggle__button">hide 01</button>
</article>
</div>
<div class="toggle">
<article>
<p>Text to be shown 02</p>
<button class="js-toggle__button">Show 02</button>
</article>
<article class="visuallyhidden js-toggle__hidden">
<p>Text to toggle 02</p>
<button class="js-toggle__button">hide 02</button>
</article>
</div>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment