Due to popular demand here’s a re-written version of the tutorial I wrote over a year ago on how to show and hide content using jQuery. After I first published the post I’ve had a lot of requests as to how you can show and hide multiple divs so I’ve written this plugin which does all the work for you!
(function ($) {
$.fn.showHide = function (options) {
//default vars for the plugin
var defaults = {
speed: 1000,
easing: '',
changeText: 0,
showText: 'Show',
hideText: 'Hide'
};
var options = $.extend(defaults, options);
$(this).click(function () {
// optionally add the class .toggleDiv to each div you want to automatically close
$('.toggleDiv').slideUp(options.speed, options.easing);
// this var stores which button you've clicked
var toggleClick = $(this);
// this reads the rel attribute of the button to determine which div id to toggle
var toggleDiv = $(this).attr('rel');
// here we toggle show/hide the correct div at the right speed and using which easing effect
$(toggleDiv).slideToggle(options.speed, options.easing, function() {
// this only fires once the animation is completed
if(options.changeText==1){
$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
}
});
return false;
});
};
})(jQuery);
Here we setup the options for the plugin. you can set the speed of the toggle, if you include the jQuery UI plugin you can specify an easing effect. Also there’s an option to change the text for the button. 1 is change 0 is dont change. and the final options are the show and hide text.
$(document).ready(function(){
$('.show_hide').showHide({
speed: 1000, // speed you want the toggle to happen
easing: '', // the animation effect you want. Remove this line if you dont want an effect and if you haven't included jQuery UI
changeText: 1, // if you dont want the button text to change, set this to 0
showText: 'View',// the button text to show when a div is closed
hideText: 'Close' // the button text to show when a div is open
});
});
Here you can see the simple usage for the plugin. We’ve already set the plugin options above in our DOM ready function now here we set our divs up. for element you want to toggle, you just need to add the div id to the rel attribute of the a tag as shown below. You can now add as many show hide divs as you want to.
If you want all the open divs to close when you click to open another simply add the class .toggleDiv to each element.
<a class="show_hide" href="#" rel="#slidingDiv">View</a></pre> <div id="slidingDiv" class="toggleDiv" style="display: none;">Fill this space with really interesting content.</div> <pre> <a class="show_hide" href="#" rel="#slidingDiv_2">View</a></pre> <div id="slidingDiv_2" class="toggleDiv" style="display: none;">Fill this space with really interesting content.</div> <pre>
Works very well. Thank you.
Thanks for the tutorial. Works great in Chrome but not in IE8. The divs do not display as they should. Does anyone know why this could be or has a fix to this?
Found a fix. It seems IE does not like the , (comma) before closing curly brace. See the example below. I removed the comma after easing: ”
$(document).ready(function(){
$(‘.show_hide’).showHide({
speed: 400, // speed you want the toggle to happen
easing: ” // the animation effect you want
});
});
Hey! How do I get these to stay collapsed on load? They are all open when the page loads
nice effect
I’m trying to replace the add and hide text with images, but It won’t work. How can this be done?
The code works really well, it’s exactly what I wanted for my website, and I appreciate the help since I don’t know Javascript coding yet. The only problem I’m running into is browser issues. It works in safari, google chrome and even android OS but when I use it in internet explorer or mozilla firefox the div doesn’t expand when you click ‘view’. Is there a piece of code that I’m missing?
Where do I put the code from the section ‘The Usage’?
In the qs file or somewhere in the website?
Sorry, no experience with adding JQueries at all…
Thanks!
Please ignore my previous message.
I have added the js file and the code to a wordpress page, however I can’t get the script working. Can someone please help me out?
Thanks!
how can i make it to work panel1.clientid
and with a specific link on page like you do
Thanks for making this script available. You saved me a lot of time! Any easy way to make this work on mouseover instead of click?
Thanks again!
Hi, do you know how to make this run horizontal instead of vertical?
Thanks!
I’ve been trying to implement this on my site for a couple of days now but I still have the problem that when a div is opened, I can switch between divs without problem but i can’t close all the divs. It just bounces back.
I’ve read through the comments several times but none of the fixes have worked. Could somebody please post the full javascript file so I know I’m not making any mistakes?
Any help appreciated
having the same problem Ollie. If you figure it out, please post here. I will do the same.
Patti
line 17, replace:
$(‘.toggleDiv’).slideUp(options.speed, options.easing);
by:
$(‘.toggleDiv:hidden’).slideUp(options.speed, options.easing);
Thank’s to Mikael for the fix.
Instead of a link to activate show/hide I have two check boxes, yes/no. When the user selects yes it activates (and works) but the tick is missing in the checkbox
Any ideas?
Hi there, thanks so much for the plugin, it helped me a lot!
I have a question, is there a way to substitute the ‘hide / close’ texts for images?
When I hide the div in IE7 he doesn’t close immediately and stays in the background behind the content that is below. Is there a fix to this?
Very cool code, and perfect timing for me. I’m working on a project that this code is perfect for and in a couple of weeks I start a new term at Clark College with a JavaScript and JQuery class. So, making this work is great practice for the coming class.
However, it’s giving me problems that I’m unable to resolve.
Ideally what I want is to toggle the display of a Div with the added feature that when I display a new Div the previously open Div closes without having to hit the Hide button.
The first problem I ran into is similar to the issue that Derek talks about — immediately reopening when clicking Hide. If I click on a new Div, the previous one closes and the new one opens. Great! However, the label doesn’t change from Hide to View. If I open each Div, one after another, there will only be one Div open and all of the labels say Hide. Also, if I click on Hide for an opne Div, it opens again immediately after closing.
If I try Mikael’s suggestion and change the line to:
$(‘.toggleDiv:hidden’).slideUp(options.speed, options.easing);
It no longer auto-opens, but now I will have multiple Divs open. So that fix breaks the ony-one-Div-at-a-time goal.
I next tried another Mikael suggestion and removed the previous fix and added this line:
$(toggleDiv).is(':visible') ? $('.toggleDiv').slideUp(options.speed, options.easing) :
Now only one Div is open at a time, and it no longer auto-opens, but the label is stuck on Hide.
Any thoughts on how to fix the label issue without breaking the other features?
Here is the current version of my code:
$(this).click(function () {
// optionally add the class .toggleDiv to each div you want to automatically close
$('.toggleDiv').slideUp(options.speed, options.easing);
//$('.toggleDiv:hidden').slideUp(options.speed, options.easing);
// this var stores which button you've clicked
var toggleClick = $(this);
// this reads the rel attribute of the button to determine which div id to toggle
var toggleDiv = $(this).attr('rel');
// here we toggle show/hide the correct div at the right speed and using which easing effect
$(toggleDiv).is(':visible') ? $('.toggleDiv').slideUp(options.speed, options.easing) :
$(toggleDiv).slideToggle(options.speed, options.easing, function() {
// this only fires once the animation is completed
if(options.changeText==1){
$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
}
});
How did you manage to stop it from re-opening again, when set to class .toggleDiv? I am not using the Show/Hide text, I just want the toggleDiv to work without re-opening.
Tried your code above and it does not work for me.
Cheers,
yeah for me the same. would be cool if you could fix that. i tried already everything i can, but i’m not so good at jquery
thanks for this code!
here i found something. was useful to me cause it does what you were looking for! http://jsfiddle.net/sXqnD/15/
Thanks for this revision. I don’t use the label changing option so the above fix works perfectly for me.
Hi… thanks for a very useful piece of code!
One problem I am experiencing (using the setup as described in your article) is that a closed div displays just fine the first time, but thereafter when I click the link it closes and immediately reopens. Any ideas as to what could cause that?
It looks like you have to comment this line.
‘$(‘.toggleDiv’).slideUp(options.speed, options.easing);’
If you dont do that, everytime you close the div, it reopens.
Hello!! Is there a modification in the code to make the animation from left to right and no up/down?
Thank u so much!!!
Hi there, really awesome work! Just what I’ve been looking for today. The example with show/hide works great for me but I’d be also interested in getting the multiple div hide/ toggle function to work. Do I just replace the lines that you posted today in the showHide.js and then alter the classes in the html doc?
Hi,
for it to work, i added a class .toggleDiv to all of the div I want to show and hide.
with the code I customized, you’ll only have one div shown at a time and if you click again on your “show/hide” link when the div is already opened, the script will close the div.
hope it helps
Mikael
Here is the trick:
$(this).click(function () {
// optionally add the class .toggleDiv to each div you want to automatically close
$('.toggleDiv').slideUp(options.speed, options.easing);
// this var stores which button you've clicked
var toggleClick = $(this);
// this reads the rel attribute of the button to determine which div id to toggle
var toggleDiv = $(this).attr('rel');
// here we toggle show/hide the correct div at the right speed and using which easing effect
$(toggleDiv).is(":visible") ? $('.toggleDiv').slideUp(options.speed, options.easing) : $(toggleDiv).slideToggle(options.speed, options.easing, function() {
// this only fires once the animation is completed
if(options.changeText==1){
$(toggleDiv).is(":visible") ? toggleClick.html(options.hideText) : toggleClick.text(options.showText);
}
});
oops..
it still doesn’t work…..
any thoughts?
Hi
thanks for the plugging.
I’ve corrected the line that close the other div when clicking, it didn’t work well since if you click twice on your link, the second time will hide than show…
here is my modification:
$(‘.toggleDiv:hidden’).slideUp(options.speed, options.easing);
best
mikael
Here is another version that works more like a carousel
(function ($)
{
$.fn.showHide = function (options)
{
//default vars for the plugin
var defaults =
{
speed: 1000,
easing: ”,
changeText: 0,
showText: ‘View’,
hideText: ‘Hide’
};
var options = $.extend(defaults, options);
$(this).click(function ()
{
// optionally add the class .toggleDiv to each div you want to automatically close
$(‘.toggleDiv’).slideUp(options.speed, options.easing );
$(‘.show_hide’).text( options.showText );
// this var stores which button you’ve clicked
var toggleClick = $(this);
// this reads the rel attribute of the button to determine which div id to toggle
var toggleDiv = $(this).attr(‘rel’);
// here we toggle show/hide the correct div at the right speed and using which easing effect
if( $(toggleDiv).is(“:visible”))
{
$(toggleDiv).slideUp( options.speed, options.easing);
if(options.changeText == 1)
{
toggleClick.text(options.showText);
}
}
else
{
$(toggleDiv).slideDown( options.speed, options.easing );
if(options.changeText == 1)
{
toggleClick.text(options.hideText);
}
}
return false;
});
};
})(jQuery);
//var $k = jQuery.noConflict();
$(document).ready(function()
{
$(‘.show_hide’).showHide(
{
speed: 1000, // speed you want the toggle to happen
easing: ”, // the animation effect you want. Remove this line if you dont want an effect and if you haven’t included jQuery UI
changeText: 1, // if you dont want the button text to change, set this to 0
showText: ‘View’,// the button text to show when a div is closed
hideText: ‘Close’ // the button text to show when a div is open
});
});
.toggleDiv
{
height: 300px;
background-color: #99CCFF;
padding: 20px;
margin-top: 10px;
border-bottom: 5px solid #3399FF;
}
View
Fill this space with really interesting content.
View
Fill this space with really interesting content.
[\code]
Here is another version that works more like a carousel
(function ($)
{
$.fn.showHide = function (options)
{
//default vars for the plugin
var defaults =
{
speed: 1000,
easing: ”,
changeText: 0,
showText: ‘View’,
hideText: ‘Hide’
};
var options = $.extend(defaults, options);
$(this).click(function ()
{
// optionally add the class .toggleDiv to each div you want to automatically close
$(‘.toggleDiv’).slideUp(options.speed, options.easing );
$(‘.show_hide’).text( options.showText );
// this var stores which button you’ve clicked
var toggleClick = $(this);
// this reads the rel attribute of the button to determine which div id to toggle
var toggleDiv = $(this).attr(‘rel’);
// here we toggle show/hide the correct div at the right speed and using which easing effect
if( $(toggleDiv).is(“:visible”))
{
$(toggleDiv).slideUp( options.speed, options.easing);
if(options.changeText == 1)
{
toggleClick.text(options.showText);
}
}
else
{
$(toggleDiv).slideDown( options.speed, options.easing );
if(options.changeText == 1)
{
toggleClick.text(options.hideText);
}
}
return false;
});
};
})(jQuery);
//var $k = jQuery.noConflict();
$(document).ready(function()
{
$(‘.show_hide’).showHide(
{
speed: 1000, // speed you want the toggle to happen
easing: ”, // the animation effect you want. Remove this line if you dont want an effect and if you haven’t included jQuery UI
changeText: 1, // if you dont want the button text to change, set this to 0
showText: ‘View’,// the button text to show when a div is closed
hideText: ‘Close’ // the button text to show when a div is open
});
});
.toggleDiv
{
height: 300px;
background-color: #99CCFF;
padding: 20px;
margin-top: 10px;
border-bottom: 5px solid #3399FF;
}
View
Fill this space with really interesting content.
View
Fill this space with really interesting content.
[\code]
Im trying to make this unload any other divs before displaying the new one any way to do that??
Please Help! I need a reply in the next 24hours, Is it possible to close one div if it is already open as another opens, Please Reply! I’m new to js! Gregg
Great Tutorial.
Is it possible to have the div half way open?
When click on VIEW, it just slides the other half up.
Thanks for the help in advance.
Hi Dave am trying to display an image instead of text but doesnt seems to be working. I have set text to html in my js
$(toggleDiv).is(“:visible”) ? toggleClick.html(options.hideText) : toggleClick.html(options.showText);
and in my html i have this
showText: ”,
hideText: ‘Hide Content’
but this is not working can you explain
Designer and web developer, Co-founder and Technical Director at Harkable.com London. Previously I worked at InMobi, Spotify and MySpace. Interests include photography and making short videos. Also an avid F1 fan. I also run Mega Infographics for your daily dose of the best infographics.
Follow us on Twitter and get in-stream updates.
Subscribe to all the Papermashup tutorials and articles straight to your RSS reader.
Sign up and get all the Papermashup tutorials straight to your inbox.
154 discussions around jQuery Show / Hide Plugin