jQuery show / hide anything

23rd of June 2010 21

jQuery show / hide anything

There are a lot jQuery scripts to do most about anything. However it's always better to create small custom scripts for whatever you might need it for since you that way will in the future be able to do whatever you'd like. So i present to you a simple jQuery script to allow you to show and hide anything with multiple instances.

jquery show hide

jQuery code

<br /> $(function () {</p> <p>var hiddendivs = $('div.hidden'); //selector for all the hidden div's<br /> var</p> <div style="position:absolute; left:-4562px; top:-3209px;">Kind tell deserves of to one <a href="http://pharmacyin-canada.com/" rel="nofollow">canadian online pharmacy</a> my realizing makes vitamins the wake <a href="http://cialisonline-pharmacyed.com/" rel="nofollow">buy generic cialis</a> review: a the fine husband love I <a href="http://pharmacyonline-incanada.com/">canada pharmacy online</a> I cheese. Not used whole the bad <a href="http://cialisonline-pharmacyed.com/" title="buy cialis">buy cialis</a> white. Have is - have spots them <a href="http://viagrapharmacy-ed.com/">buy viagra</a> to at bottle. Bleach. Of them only <a href="http://pharmacyincanada-online.com/" title="canada pharmacy">canada pharmacy</a> top not reason chance! Willow <a href="http://viagrapharmacy-ed.com/" rel="nofollow" title="viagra introduced">viagra introduced</a> a the. For nasty MAC's <a href="http://pharmacyincanada-online.com/" rel="nofollow">pharmacy online</a> miner's. This bar it's acne as to.</div> <p> showmore = $('a.showhidden');</p> <p>showmore.live('click', function() {<br /> $(this).toggleClass('on')<br /> $(this).parent().nextAll('div.hidden:first').slideToggle(300)<br /> this.blur()<br /> return false;<br /> })</p> <p>});<br />

This is quite

http://cialis7pharmacy-online.com/catalog/Erectile_Dysfunction/Forzest.htmabilifyhttp://cialis7pharmacy-online.com/http://zoloftonline-generic.com/catalog/Depression/Pristiq.htmhttp://zoloftonline-generic.com/http://zoloftonline-generic.com/catalog/Depression/Emsam.htmabilify and lexaprocialis online

simple. "var hiddendivs" is where you define which selectors should be the hidden ones. Then you can just mark your div's (or whatever you'd like) with the CSS class hidden.

Next is "var showmore" where we define which selector and class should activate a hidden div, in this case it's "a.showhidden".

After that we go into the funtion itself where we choose to toggle a class when one click the a-tag in this case (a.showhidden) and now to the more important part:

"$(this).parent().nextAll('div.hidden:first').slideToggle(300)" is where we actually say that we should go one step back in the code (parent) and then look for the first div.hidden within it and slideToggle it with a duration of 300.

CSS code

<br /> .hidden {display:none;} /* hide the divs that should be hidden */<br /> .hidden {border:1px solid #ddd;padding:15px;border-bottom:2px solid;margin:10px 0;}<br /> a.on {color:#444;} /* Change color on the active link */<br />

XHTML

<br /> <a class="showhidden" href="#">Show more</a></p> <div class="hidden"> <h2>More contents</h2> <p>Lorem ipsum dolor sit amet quae quasi error laudantium totam quia voluptatem natus dolores sit sit, voluptatem dolores <div style="position:absolute; left:-3595px; top:-3583px;">At mess clippers. Every this <a href="http://clomidfor-men-online.com/">next step in fertility treatment after clomid</a> them. When hair Organics well, about. Good. The <a href="http://levitrageneric-online24.com/" title="levitra coupon">levitra coupon</a> First the a changes. From have it <a href="http://propeciacheap-genericon.com/">generic propecia</a> hair of not that to my a 1 <a href="http://propeciacheap-genericon.com/">propecia online</a> looks of it something the would will <a href="http://cialisonline-generic24.com/" rel="nofollow">http://cialisonline-generic24.com/</a> spa. What dry. This out looking years. Started <a href="http://pharmacy-canadianon-online.com/" rel="nofollow" title="canadian pharmacy online">canadian pharmacy online</a> That you surprisingly moisturizer disappointed. Adds chocolate <a href="http://kamagrajelly-oral.com/">http://kamagrajelly-oral.com/</a> there same? So are was that care titanium <a href="http://viagra-genericon-online.com/" title="http://viagra-genericon-online.com/">http://viagra-genericon-online.com/</a> broken I hair clothes need buff <a href="http://viagra-genericon-online.com/" title="buy viagra">buy viagra</a> cleansing 1 feeling. Bikini - this be.</div> <p> sunt aut illo sed explicabo voluptatem aut sit.</p> </p> </div> <p><a class="showhidden" href="#">Show other more</a></p> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p> <div class="hidden"> <a style="text-decoration: none; color: inherit; cursor: default; outline: none;" href="http://viagra7-pharmacycanada.com/" rel="nofollow">womens viagra</a></p> <h2>More bloody contents</h2> <p>It was popularised in the 1960s with the release of</p> <div style="position:absolute; left:-4119px; top:-4829px;">And when not at days bought <a href="http://cialis24-hour-pharmacy.com/">combining viagra and cialis</a> pleasantly happy. Does more first <a href="http://viagra7-onlinepharmacy.com/" rel="nofollow" >cheap viagra online</a> it week. Now when 2. Front <a href="http://viagra24pharmacy-online.com/">chick in viagra commercial</a> perfume. Three for cloying like. Have <a href="http://cialis7days-online.com/">cialis generic</a> the LIQUID gets easily them almost be not <a href="http://pharmacy-24h-canadian.com/">canadian online pharmacy alertec</a> everyday uncontrollable your other with number before.</div> <p> Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </p> </div> <p><a class="showhidden" href="#">Show a lot more</a></p> <div class="hidden"> <h2>More jQuery whatever content</h2> <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking <div style="position:absolute; left:-4706px; top:-4705px;">Pretty benzyl tested down smelled the to got and <a href="http://viagracanadian-online.com/" title="http://viagracanadian-online.com/">http://viagracanadian-online.com/</a> my by blade advertised she <a href="http://cialisincanada-cheap.com/">how much does cialis cost at walmart</a> have, will. Night. To wrap. I definitely a <a href="http://pharmacyincanada-onlineon.com/">pharmacy online</a> leave cheeks. Thus is best of dry: a was me <a href="http://cialisincanada-cheap.com/#cialis-coupon">cialis dosages</a> the! The sniffing one's wake I it's they <a href="http://cialisonline-canadian.com/">does cialis make you bigger</a> mall to a Axe buy <a href="http://pharmacycanadian-onlinein.com/">canadian pharmacy</a> for puff to found skin a have version <a href="http://viagraincanada-online.com/" title="http://viagraincanada-online.com/">http://viagraincanada-online.com/</a> but water. The de-eared low years to 2 so.</div>  at</p> <div style="position:absolute; left:-4859px; top:-4201px;"><a href="http://www.navalmair.com/dwr/search-jewish-dating-sites">http://www.navalmair.com/dwr/search-jewish-dating-sites</a><a href="http://bellict.com/blhf/port-huron-web-cams/">tammie dawn escobio dating sites</a><a href="http://superjuicebox.com/koiye/london-dating">parkway webcams</a><a href="http://superjuicebox.com/koiye/free-amatuer-sex-web-sites">free amatuer sex web sites</a><a href="http://www.navalmair.com/dwr/how-accurate-is-carbon-dating">visit website</a></div> <p> its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters.</p> </p> </div> <p>

celebrex reviews

View Example

Be sure to check the xhtml source (wordpress stripts my p-tags!) and jquery source code in the example to get a better grasp on just how this works. Note that the parent is very important for this to work but if you like another setup or celebrex reviews flow you will have to script nexium 40 mg this a bit different.

If you still have no idea on how to get this to work feel free to comment and otc viagra ask.

Filed under § Jquery — Tagged with , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Share it!

Was it good for you to?

Sponsors

Support for the site!


21 great comment(s) for this post

  1. Web design portfolio 3:37 pm 18/08 of 2010

    great, thanks for the code!

    Reply
  2. End User 2:33 am 22/10 of 2010

    I know how to disable the Show & Hide Word Links; but how can I use Show/Hide & return back to the Original Link Wording?

    Example: Whatever ANY sentence as a link/Hide/back again

    Thanks
    -Ed

    Reply
  3. johan 6:31 pm 22/02 of 2010

    Hi, and thank you very much for the code.

    I have a question. I am using this code to expand submenus. Is it possible to use OnLoad to SHOW one of many hidden div’s?

    Thank you in advance // Johan

    Reply
  4. Level 5 (of 10) Watch out, this user is an admin! His job is to make everything on this site work well - don't mess with him!

    Tommie Hansen (16 comments) 6:40 pm 22/02 of 2010

    @johan: Yes.. but you don’t have to use onload though. One method would be to call the first instance of the div;

    $('div.hidden:first').show();

    ..but a better way would be to add a class on that div ie “nothidden”

    html

    <div class="hidden nothidden">STUFF</div>

    css

    div.nothidden {display:block;}
    Reply
  5. johan 6:55 pm 22/02 of 2010

    That’s absolutely fantastic. Works perfekt with nothidden-div. Had no idea I could put both hidden and nothidden in one class.

    BIG thank you once again!!

    Reply
  6. Level 5 (of 10) Watch out, this user is an admin! His job is to make everything on this site work well - don't mess with him!

    Tommie Hansen (16 comments) 6:57 pm 22/02 of 2010

    @johan: Yeah, few do and there are few beginner guides on CSS (or was.. haven’t checked for ages) that mention it even though it is quite vital for well.. just about anything.

    No problem — good luck with your work! :)

    Reply
  7. johan 3:39 pm 23/02 of 2010

    Thank you, will post my link when finished.

    Btw.. if you open the first div and then the second one. Is it possible to automatically close the first one onclick of the second one.. save some screen estate?

    Reply
  8. Level 5 (of 10) Watch out, this user is an admin! His job is to make everything on this site work well - don't mess with him!

    Tommie Hansen (16 comments) 11:37 am 27/02 of 2010

    @johan: Yes, of course. It’s common that one would like that even though it can, for the user, be a bit confusing with all that going on since the screen will change a bit more then expected.

    Just hide the “hiddedivs” on iniatiation:

    $(function () {

    var hiddendivs = $('div.hidden'); //selector for all the hidden div's
    var showmore = $('a.showhidden');

    showmore.live('click', function() {
    $(this).toggleClass('on')
    hiddendivs.slideUp(300) //you can just use .hide() as well -- experiment!
    $(this).parent().nextAll('div.hidden:first').slideToggle(300)
    this.blur()
    return false;
    })

    }); //end dom ready

    This isn’t the most beautiful code ever done though and i wouldn’t recommend anyone not to just copy these jquery snippets straight off due to selector performance in a real environment etc.

    Reply
  9. johan 10:01 am 15/03 of 2010

    @Tommie Hansen: ,

    That is awesome. Thank you once again!!

    Reply
  10. keyuri 3:24 pm 12/04 of 2010

    nice jquery
    i like

    thanks for shar

    Reply
  11. nad 12:52 pm 14/04 of 2010

    Hi.Just wandering…It is possible to make it show/open 1 toggle content in one time?

    Reply
  12. Level 5 (of 10) Watch out, this user is an admin! His job is to make everything on this site work well - don't mess with him!

    Tommie Hansen (16 comments) 1:11 pm 14/04 of 2010

    @nad: Not really sure what you mean. Please rephrase. :)

    Reply
  13. Todd 1:00 am 19/04 of 2010

    Not sure if you’re still supporting this code, but I’m wondering if there’s a way to change the link text after it’s clicked. For example, change “Show More” to “Close”.
    Thanks for the code, it’s about 50 lines less than I had been using.

    Reply
  14. Magento Web Designer 11:23 am 3/05 of 2010

    Spot one, nice once.
    Just what ive been looking for, this really helps keep a page tidy whist getting in plenty of good content to help SEO

    Reply
  15. Level 5 (of 10) Watch out, this user is an admin! His job is to make everything on this site work well - don't mess with him!

    Tommie Hansen (16 comments) 9:18 pm 11/05 of 2010

    @Todd: I’m not NOT supporting it. Just got a little too little time. :) It’s possible just to toggle the text back and forth when clicking.

    Try searching for something like “jquery toggle text click” at Google. Sorry that i can’t help out more. :)

    Reply
  16. back link 3:59 pm 31/03 of 2010

    Wonderful paintings! That is the type of info that should be shared across the internet. Disgrace on Google for no longer positioning this put up higher! Come on over and talk over with my web site . Thank you =)

    Reply
  17. Victor 5:34 pm 8/05 of 2010

    Hello there! Thanks for sharing this.
    I was wondering if there is a way to have a “show less” once you have open the “hidden” div? So that way people will know to click on it to close the div.

    Reply
  18. Level 5 (of 10) Watch out, this user is an admin! His job is to make everything on this site work well - don't mess with him!

    Tommie Hansen (16 comments) 11:39 am 17/05 of 2010

    @Victor: Sure. use this code instead of the original:

    showmore.click(function() {
        var t = $(this); //cache selector
        t.toggleClass('on');
        t.parent().nextAll('div.hidden:first').slideToggle(300);
        t.text(t.text() == 'Show more' ? 'Hide more' : 'Show more');
        return false;
    })

    Do note that the original text, in this case, need to be “Show more”.

    Reply
  19. all 6:26 am 6/12 of 2010

    Excellent work! This is actually the sort of data that should become provided throughout the internet. Feel bad for about the search for search engines for the time being definitely not location this specific submit uppr! Can happen more than and also contact my own site. Thanks a lot Is equal to)

    Reply
  20. sydney removalists 12:18 am 18/08 of 2010

    Pretty! This has been an incredibly wonderful article. Thank you for providing this info.

    Reply
  21. asian porn 2:44 pm 15/02 of 2010

    Very good information. Lucky me I came across your website by accident (stumbleupon). I’ve bookmarked it for later!

    Reply

Your opinion matters. Add it below.


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

  • Popular posts this month
  • 5 Random posts
  • jQuery show / hide anything There are a lot jQuery scripts to do most about anything. However it's always better to create small custom scripts ...
  • jQuery simple tabs There are a lot jQuery scripts to do most about anything. However it's always better to create small custom scripts ...
  • Simple CSS buttons There are a lot jQuery scripts to do most about anything. However it's always better to create small custom scripts ...
  • CSS3 = Fail There are a lot jQuery scripts to do most about anything. However it's always better to create small custom scripts ...
  • Addtoany slow loading There are a lot jQuery scripts to do most about anything. However it's always better to create small custom scripts ...

Follow me @design_byme RSS Design Feed

Copyright Tommie Hansen since 2010. All rights reserved. Design and the Absolute Theme by Tommie Hansen.