0

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');
     });
 });
2
  • Isn't his a basic accordion that you're after? Commented Jul 2, 2013 at 13:53
  • @j08691 yes if that is the correct term for it Commented Jul 2, 2013 at 13:54

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

@NewKidOnTheBlock Yes, me too. Don't forget to mark it if you consider the correct answer.
1

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

Comments

0

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');
         }
         });
     });

1 Comment

in the rush of having your question answered first i didn't think of using .slideToggle and .toggleClass, the code above will still do the job perfectly, however for shorter code please look at Claudio Redi's answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.