jQuery Simple Collapsible Div?

Aaron Brewer picture Aaron Brewer · Dec 13, 2011 · Viewed 96.1k times · Source

I am looking for the proper, simple, small code to do the following things:

  • Click on Element with Class Applied to it.

  • DIV.CLASS - Which expands and shows hidden content. (slideDown - Toggle)

  • DIV.CLASS - Which collapses and hides the previously show content. (slideUp - Toggle)

I have created a jsFiddle here: http://jsfiddle.net/Q4PUw/1/

So to be vague and easy, I need to know how to get a DIV CLASS to become hidden and visible once an element on the same page has a CLASS applied to it, in which would activate and deactivate the HIDDEN and or VISIBLE HTML Content. And I need it to be hidden by default.

I have looked all over the internet and have only found very complex scripts, but nothing simple. I have found Simple Accordians... But those never close, they just open another one.

Thank you all for the help and I hope that I may be able to answer this same question for plenty of other people...

Note: I know very little JavaScript and or even jQuery.

Answer

AlienWebguy picture AlienWebguy · Dec 13, 2011
$('.expand-one').click(function(){
    $('.content-one').slideToggle('slow');
});

Demo: http://jsfiddle.net/Q4PUw/2/