Use your left/right keys to browse tutorials
Simple jQuery Show/Hide Div

Simple jQuery Show/Hide Div

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on June 2, 2009

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

The JavaScript

<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>

The HTML

<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>

The CSS


.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

demo download



Recent shares

More tutorials from Papermashup
Comments
188 discussions around Simple jQuery Show/Hide Div
Older Comments
  1. MLM Software says:

    Thanks to share this article. that is very help full me

  2. Alex says:

    Is it possible to have it popping up on different Div or does it work only with one Div?

  3. Alex says:

    Is it possible to not make it a slider? And just have it pop up?

  4. eko says:

    thanks,helpfull

  5. James says:

    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.

  6. Pravin says:

    Thanks.. It helped me

  7. This was simple and useful even for a JQuery illiterate bum like me!

  8. Sarah says:

    It helps me a lot for hiding answers from a quiz
    Thansk a lot for sharing

  9. Arun says:

    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

  10. Nick says:

    man thanks a lot save me a lot of time.

  11. Stefano says:

    Nice and clean, thank you.

  12. Awesome tip, and very easy to customize
    once you understand how the script works!

    Thanks for sharing!

  13. Kyle says:

    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?

  14. Eckstein says:

    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?

  15. Minh Hien says:

    Great! Thanks a lot!

  16. ravinder says:

    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.

  17. Maninder Singh says:

    Thanks Very Much

  18. P-teo says:

    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.

  19. Iosif says:

    Many thanks for the code. It is the simplest and nicest one I could find. It works great.

  20. Pingback: Prototype 4: the hidden div strikes back « LRD on DMT

  21. Billy says:

    Thanks for share, i used it for my website, demo http://mylyric.net

  22. Rajan says:

    Thanks a lot

  23. gine8 says:

    Many thanks!! Easy and very helpfull!

    ;-)

  24. Really very very nice article to show and hide the same div on click of same div…..
    Great work and thnx…

  25. Puneeth says:

    Hi,

    Thanks a lot…

  26. dswww says:

    Hi, Thanks for the article. I will try make somethime like this div.

  27. Nigel says:

    Thanks, very handy.

  28. adam says:

    Just wanted to share the website named: http://www.uxzeal.com/… dedicated to designing community…

  29. tom says:

    Is there a way of making the div visible as the default view when the page loads?

  30. sam says:

    Very useful one

  31. mahmud says:

    Thanks for analyzing everything. Is there any way to include a contact form in the hidden div?

  32. Hey, Thanks a lot for this nice snippet

  33. Pramod says:

    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!!

    • Miraj says:

      use style=”display:none” while declaring div which you want to toggle and then use the above javascript code.

  34. Muhsin says:

    thanks
    The code and java script is very good

  35. Muhsin says:

    The code and java script is very good

  36. suhail says:

    thanx for this article…………

  37. Bhaksar says:

    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

  38. Sardar Ranjeet Singh says:

    Thank you so much, it was very helpful.

  39. Ulrika says:

    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.

  40. Ava says:

    Thank you so much! This has saved me a bunch of time. Great work.

  41. Hi, Thanks for the article. Great post.

    The ‘#’ link takes the user to the top of the page. Is there anyway to disable that?

  42. Valeka says:

    Thanks a lot! Great job here!

  43. Francis L says:

    A big help when I needed a quick reference as a refresher. Thanks!

  44. Melissa says:

    Bravo! Exactly what I was looking for. Easy to understand.

  45. Al says:

    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?

  46. Dave says:

    Hi!

    Thanks for the tutorial. Can you tell me how I start with the div hidden?

    Dave

  47. chris says:

    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?

  48. Davinder Singh says:

    not working in blogger :( please tell me how to use this in blogger

  49. Osward says:

    This tool can it be used even if script is not enabled? And how do i use it within one page several times?

    • Ashley says:

      @Osward,

      nope this script requires JavaScript to be enabled, and to use it multiple times just assign a class to the div you want to show / hide.

      Ashley

  50. Mark says:

    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.

  51. mike says:

    thnx for the code, working great

  52. venkat says:

    thanks for your code ,its simple and working.

  53. San says:

    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!!

  54. rajiv says:

    thankx for giving me jquery its working

  55. migs says:

    hi there,

    can this have a function to hide all the other divs when i click on to show one div?

    Thanks

  56. andrea says:

    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

  57. Anantha.KN says:

    Thanks a Lot

  58. Janitha says:

    Coolest Div Show & Hide Script ever. Thankx…

  59. Uday says:

    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!

  60. Danny says:

    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!

  61. jackm says:

    Thank you, it’s cool tutorial helped for me.

  62. 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…

  63. RASUI71 says:

    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. ;)

  64. koushik says:

    @mike
    use the slidetoggle function. If your div is in show status it will automatically slide up to hide the div (footer).

  65. mike says:

    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 :-)

  66. Chem says:

    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.

  67. Hi,

    Thanks to share this article.

  68. Pat says:

    Hi Ashley!
    Thank you so much for this new article!

Older Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.