Here’s a simple tutorial on how to show/ hide a div using jQuery. First we include the Google jQuery library, the next steps simple. We now setup a click event listener which is triggered when a users click the and object that has the class ‘show_hide’ which then toggles the div element with the class ‘slidingDiv’. In the HTML below we’ve setup a simple link with the class ‘show_hide’ and the content div in which we’ll show and hide which has the class ‘slidingDiv’. You’ll also notice in the first two lines of JavaScript we’re hiding the div content and showing the href this is so if the user doesn’t have JavaScript enabled they see the content box open but no option to hide it.
Hold up! Are you looking to show and hide multiple divs? Check out the latest tutorial on how to do this using a plugin i’ve written for this very job. Get the plugin here
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".slidingDiv").hide();
$(".show_hide").show();
$('.show_hide').click(function(){
$(".slidingDiv").slideToggle();
});
});
</script>
<a href="#" class="show_hide">Show/hide</a> <div class="slidingDiv"> Fill this space with really interesting content. <a href="#" class="show_hide">hide</a></div>
.slidingDiv {
height:300px;
background-color: #99CCFF;
padding:20px;
margin-top:10px;
border-bottom:5px solid #3399FF;
}
.show_hide {
display:none;
}
Although this code technique is simple its often required in projects.
Hold up! Are you looking to show and hide multiple divs? Check out the latest tutorial on how to do this using a plugin i’ve written for this very job. Get the plugin here
Thanks to share this article. that is very help full me
Is it possible to have it popping up on different Div or does it work only with one Div?
doesn’t matter. sorted this issue
Is it possible to not make it a slider? And just have it pop up?
thanks,helpfull
If your site is run over https, this won’t always work. The fix is super trivial. Just change that jquery reference to:
You could just leave it as that in general… If you’re one of the cool kids, like me.
Thanks.. It helped me
This was simple and useful even for a JQuery illiterate bum like me!
It helps me a lot for hiding answers from a quiz
Thansk a lot for sharing
Really nice article. You can check this link which has an article on how to Show, Hide an HTML Div and Animate an Asp.Net “Panel” Control.
http://www.encodedna.com/2013/02/jquery-show-hide-div-animate-panel.htm
Your reviews will be appreciated.
Thank you.
Arun
man thanks a lot save me a lot of time.
Nice and clean, thank you.
Awesome tip, and very easy to customize
once you understand how the script works!
Thanks for sharing!
I copied the code to a site. For Only thing I did was change the div class name. The div starts hidden, when I click on the link the div shows. However, when I click again to hide the div, it doesn’t hide.. It goes away for a split second but shows right back up. Thoughts?
Is there a way to only show 1 div at a time. So that if a second one is opened, it closes the first, etc?
Try this link for a clue.
http://www.encodedna.com/2013/02/jquery-show-hide-div-animate-panel.htm#divDemo
On the click of first button, you can close the secode div
Great! Thanks a lot!
Hey can someone please help me to use this code in blogger i want use it in one of my blog please tell me how to use this code.
Thanks Very Much
Can anyone explain why it has the
$(“.slidingDiv”).hide();
$(“.show_hide”).show();
Using display:none; in the css does it fine from what i have seen.
Many thanks for the code. It is the simplest and nicest one I could find. It works great.
Pingback: Prototype 4: the hidden div strikes back « LRD on DMT
Thanks for share, i used it for my website, demo http://mylyric.net
Thanks a lot
Many thanks!! Easy and very helpfull!
Really very very nice article to show and hide the same div on click of same div…..
Great work and thnx…
Hi,
Thanks a lot…
Hi, Thanks for the article. I will try make somethime like this div.
Thanks, very handy.
Just wanted to share the website named: http://www.uxzeal.com/… dedicated to designing community…
Is there a way of making the div visible as the default view when the page loads?
Very useful one
Thanks for analyzing everything. Is there any way to include a contact form in the hidden div?
Hey, Thanks a lot for this nice snippet
This is good hide show example, i have used this in my work, its working perfectly. but seems bit issue. Whenever you refresh the page the hidden div will popout and go. how can we get rid of the popup when we refresh the page.
Thanks!!
use style=”display:none” while declaring div which you want to toggle and then use the above javascript code.
thanks
The code and java script is very good
The code and java script is very good
thanx for this article…………
hi
great post and easy to understand.I have done on this based on drop down list selection
http://csharpektroncmssql.blogspot.com/2011/12/hideshow-div-using-jqueryjson.html
Thank you so much, it was very helpful.
Hi!
Thanks for this good tutorial and code, very good!
I wonder if this is possible:
From another page I want to link to this page and in the link tell what div should show on the landing page.
Thank you so much! This has saved me a bunch of time. Great work.
Hi, Thanks for the article. Great post.
The ‘#’ link takes the user to the top of the page. Is there anyway to disable that?
Hi there Nima.
If you replace the href=”#” with something like this: href=”javascript:void(0);” then the browser shouldn’t jump to the top of the page.
Cheers
Miked
Thanks for asking what I was going to ask
Thanks a lot! Great job here!
A big help when I needed a quick reference as a refresher. Thanks!
Bravo! Exactly what I was looking for. Easy to understand.
Hi Ashley
This works really well. However, I just wondered if there was a way I could have the ‘show/hide’ disappear when the div is expanded?
Hi!
Thanks for the tutorial. Can you tell me how I start with the div hidden?
Dave
hi, can anyone tell me, how to do, if i have several links, if one of the links / divs is open, if i click on another link, the previous one to close, and to open the new div? how to do that?
this is for test
not working in blogger
please tell me how to use this in blogger
This tool can it be used even if script is not enabled? And how do i use it within one page several times?
Symantec detected a malicious cookie on your site.
[SID: 25556] Web Attack: Malicious Cookie Activity 4 detected. Traffic from IP address 146.185.254.146 is blocked. Also your contact form does not work, at least with IE8. Just an Fyi.
thnx for the code, working great
thanks for your code ,its simple and working.
Thanks for this tutorial! Is it possible to make it show from bottom to top instead of from top to bottom? I mean the sliding animation, since I’m making a footer with buttons. Thanks!!
thankx for giving me jquery its working
hi there,
can this have a function to hide all the other divs when i click on to show one div?
Thanks
hi, great script.
i have a question. How can i set before the DIV “hide” then when click the DIV “show”? in short, on the contrary.
hope your help.
regards
andrea
Thanks a Lot
Coolest Div Show & Hide Script ever. Thankx…
Hi, I need to write similar effect if user checks… a radio button. If he clicks ‘Yes’ then it should show else it shouldnt show. Thanks!
Thanx for the script. I got it working in less than half an hour. I use it to make the subnav standard nog longer than 10 items. Clicking on the “show more” button shows the rest of the items.Superb!
Thank you, it’s cool tutorial helped for me.
hi anyone could help me…. how about i have 100 title with hide content and show..is any one could know the array on it?
like this one..
$(“.slidingDiv”).hide();
$(“.show_hide”).show();
$(‘.show_hide’).click(function(){
$(“.slidingDiv”).slideToggle();
how could i make 100 DIV to make it short the code…I hope you could response with me
thanks again…
Hi there,
Would someone help me to do some modification to the below mention code. I’m just a beginner. The below code does the hide and view part (div). But i want to do the same for many div tags. Ex. clickHere1 for detail1, clickHere2 for detail2, clickHere3 for detail4 etc. to view different info under each div.
_____________________________________________
$(‘#clickHere’).click(function() {
$(‘#details’).slideToggle(‘slow’, function() {
// Animation complete.
});
});
_____________________________________________
Thanks.
@mike
use the slidetoggle function. If your div is in show status it will automatically slide up to hide the div (footer).
Hi, can anyone let me know how to get the div to slide up instead of down, as im putting it in a footer?
thanks in advance
perfect! Thank you!
hi,
great article but its not working on my IE 7 browser. It works in chrome, FF, IE8 but not IE7.
Can you guide please.
Hi,
Thanks to share this article.
Hi Ashley!
Thank you so much for this new article!
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.
188 discussions around Simple jQuery Show/Hide Div