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

Hide and show on click

Hello!

In the project "Build an Interactive Website" --> "Introduction to jQuery" --> "Including jQuery in our Project" we add to the section of Cupcakes & Prices (of the Smells Like Bakin website) the ability to show the order form when the user clicks the order button with this code:

$(".order_form").hide();

$(".order_now").click(function() {

var $link = $(this);

$link.next().show("slow");

return false;

})

In my own project, the link doesn't disappear (in this case, the .order_now) unlike the Smells Like Bakin project.

My question is, how would the code be if I want that when the user click again in that link, the order form disappear again?

Thanks! :)

2 Answers

Kevin Korte
Kevin Korte
28,148 Points

jQuery's toggle() method might be what you need.

Here is an ugly and crude sample, but makes the case.

http://codepen.io/anon/pen/znwAy

And the API

http://api.jquery.com/toggle/

Perfect! Thanks :)