NewKidOnTheBlock
NewKidOnTheBlock

Reputation: 1511

jquery Show and hide menu issue

I am pretty new to Jquery so forgive me for the simple question. I have a created a mulitple show and hide menu, which works fine, however i want to add an extra function to it. When click on for example the blackJack 1 slots it it opens the dropdown menu. when i click on it whilst open it close the menu also. Looking at my code does anyone know how this could be achieved?

At present the current works when you click on "slot" it opens the next slot and closes the previous slot.

HTML

<div class="drop-inner slots">
    <h2>Blackjack</h2>
</div>

<div class="hide-me">
    <p>content goes here</p>
</div>


<div class="drop-inner slots">
    <h2>Blackjack 2</h2>
</div>

<div class="hide-me">
    <p>content goes here</p>
</div>

<div class="drop-inner slots">
    <h2>Blackjack 3</h2>
</div>

<div class="hide-me">
    <p>content goes here</p>
</div>

Javascript

$(function () {
     $('.slots').click(function () {
        var $this = $(this);
        $('.hide-me').slideUp().removeClass('active');
        $this.next('.hide-me').slideDown('slow').addClass('active');
     });
 });

Upvotes: 0

Views: 164

Answers (3)

Ionică Bizău
Ionică Bizău

Reputation: 113365

Verify if the $next element is visible or not.

$('.slots').click(function () {

    var $this = $(this);
    var $next = $this.next('.hide-me');

    $('.hide-me').slideUp().removeClass('active');
    if ($next.css('display') !== 'none') {
        $next.slideUp('slow').removeClass('active');
    } else {
        $next.slideDown('slow').addClass('active');
    }
});

JSFIDDLE

Upvotes: 1

Claudio Redi
Claudio Redi

Reputation: 68400

This could do the trick

$(function () {
    $('.hide-me').hide();
    $('.slots').click(function () {
        var $this = $(this);
        var $thisHideMe = $this.next('.hide-me');
        $('.hide-me').not($thisHideMe).slideUp().removeClass('active');
        $thisHideMe.slideToggle('slow').toggleClass('active');
    });
});

DEMO

Upvotes: 1

Raj Nathani
Raj Nathani

Reputation: 2845

Here : http://jsfiddle.net/nexk5/1/ :)

 $(function () {
     $('.slots').click(function () {
         var $this = $(this);
         var $next_hideme = $this.next('.hide-me');
         $('.hide-me').not($next_hideme).slideUp().removeClass('active');
         if (!$next_hideme.hasClass('active')) {
             $next_hideme.slideDown('slow').addClass('active');
         } else {
             $next_hideme.slideUp('slow').removeClass('active');
         }
         });
     });

Upvotes: 0

Related Questions