Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.