Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

troy beckett
troy beckett
12,035 Points

advice on hide and showing divs??

please take a look at my codepen below:

http://codepen.io/tbeckett24/pen/OPYQmM

So I've got two divs stacked on top of each other, with a button above called show second.

What I want is for the red div to be showing first and the green div not visible. Then when I click the button I want the red div to hide completely and the green div to show. and vice versa when I click the button again.

I'm guessing this will involve jquery or javascript and I'm gonna try and learn it myself, but if anyone has an idea or has done it before and push in the right direction would be great thanks guys.

2 Answers

Colin Bell
Colin Bell
29,679 Points

It can definitely be done with pure CSS, but more than likely you're going to want to use JavaScript/jQuery.

The easiest way to do it would be to set the default display of all the divs you want to toggle to none, then toggle a class that sets the display of the active div to block.

codepen

troy beckett
troy beckett
12,035 Points

thanks alot for your reply it really helped can I just ask you how you learnt jquery/javascript. I'm trying to learn it now and I'm finding it a lot harder than with css. I mean is it just keep trying or did you learn it in a particular way. I guess I mean if you had to learn it again how would you go about it. Thanks again for your comment.

Colin Bell
Colin Bell
29,679 Points

I started out learning vanilla JavaScript and I'd strongly suggest doing that. Build a good foundation with that, and jQuery will come much easier. It was important to me to know why things work instead of just how things work.

I followed this guide: How to learn JavaScript properly and mixed in the Treehouse tutorials. That link has paid courses now, but if you scroll down to here they have two different study guides with free resources and a $30 book. Both books are well worth it, and if I had to do it again I'd probably do it the same way.

But a lot of it is just practice. I'd think of something that could maybe be done with JS and tried figure out a way to do it. If I did it then I'd try to refactor the code to get it as simple and reusable as possible.

StackOverflow.com is an invaluable resource. Chances are if you're having a problem, someone else has had it already and asked about it there. If they haven't, then ask yourself and somebody there is going to know how to help you.