Yammi
Yammi

Reputation: 735

jQuery - Close an already open toggle if clicked on

Had a look around on stackoverflow but can't see a similar problem.

I simply want to be able to close the open div that the toggles associated to. At the moment it just replays the toggle effect and slides it open, leaving a div always open/showing.

http://jsfiddle.net/Bx9Ya/

So if clicked, then open associated div. If clicked on again, slide associated div back up.

Upvotes: 2

Views: 3534

Answers (2)

JAAulde
JAAulde

Reputation: 19560

The problem is that before you call toggle() on the one you care to show, you're explicitly hiding ALL of these DIVs with this line of your click function:

$('[class^=toggle-item]').hide();

If you've hidden all, toggling one means to open it.

Check this fiddle where I check the visible state then make a decision based on it: http://jsfiddle.net/JAAulde/Bx9Ya/6/

Upvotes: 1

aavezel
aavezel

Reputation: 604

Like this? http://jsfiddle.net/Bx9Ya/4/ Hide all div's only when toggled element not visible ...

Upvotes: 3

Related Questions