0

I have an accordion that...

  1. When clicked i want the next dd section to expand.
  2. If another is clicked i want all the other open sections to close first
  3. If an open title is clicked Id like to close...

I have the following example that I cant get working...

jQuery

$(".accordion > dt > a").click(function () {
    $(".accordion a").removeClass('active');
    if ($('.accordion > dd').next().is(':visible')) {
        $(this).removeClass('active');
        $('.accordion > dd').slideUp(200);
    } else {
        $(this).addClass('active');
        $(this).parent().next().slideDown();
    }
});

http://jsfiddle.net/m0zehxpc/

3
  • 1
    Perhaps you can show a fiddle? Commented Jun 10, 2015 at 6:53
  • Add your HTML code Commented Jun 10, 2015 at 6:55
  • I think one of the problems is that you are not allowing the else condition to work. And also the last rule is your CSS looks faulty. Commented Jun 10, 2015 at 7:17

5 Answers 5

1

Something like this

$(".accordion > dt > a").click(function () {
    $(".accordion a").removeClass('active');
    $(this).addClass('active');
    $('.accordion > dd').not($(this)).slideUp(200);
    $(this).parent().next('dd').slideDown(200)
});

You can just ignore .not part in this

$('.accordion > dd').not($(this)).slideUp(200);
Sign up to request clarification or add additional context in comments.

8 Comments

May i know the meaning of $('.accordion > dd').not($(this)).slideUp(200);
@papsk Except the clicked element's dd slide up all the other dd but that seemed irrelavant here as he has close everything first and then open the clicked one.
Thank you ji...but i can't understand this...initially all are closed only na...then why it is irrelevant
@papsk It will be useful when you open one accordion and when you click other you might need to close the opened one!
not(":even") does not consider the elements which are at even index when referred from their parent!!
|
0

I have fixed few things in your code and now it works like any other standard accordion.

JS Code :

 $(".accordion > dt > a").click(function () {
 var $this = $(this);
 var content = $this.parent().next();
 if (!$this.hasClass('active')) {
     $(".accordion a").removeClass('active');
     $('.accordion > dd').slideUp(200);
     $this.addClass('active');
     content.slideDown();
 } else {
     $this.removeClass('active');
     content.slideUp(200);
 }
});

Live Demo @ JSfiddle:

http://jsfiddle.net/dreamweiver/m0zehxpc/12/

Comments

0

Try this code :

$(".accordion > dt > a").click(function () {
     var $myDD = $(this).parent().next();
     $(".accordion a").removeClass('active');
     $(this).addClass('active');
     $('dd').slideUp();
     
     if ($myDD.is(':visible')) 
         $myDD.slideUp();
     else 
         $myDD.stop().slideDown();
 });
.about-us .accordion {
    padding: 0 1.6em;
    max-width: 970px;
    margin: 0 auto 50px auto;
}
.about-us .accordion dt {
    margin: 0 0 5px 0;
}
.about-us .accordion dt a {
    background:#EDEFEF no-repeat scroll right 0px !important;
    color: #4e5663;
    display: block;
    text-transform: uppercase;
    padding: 20px;
    text-decoration: none;
    font-weight: 500;
    font-size: 16px;
}
.about-us .accordion dd {
    display: none;
    padding: 20px;
    margin: -7px 0 5px 0;
    border: 2px solid #00b0e4;
}
body.about-us .accordion a.active {
    background:#00B0E4 no-repeat scroll right -67px!important;
    color:#fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="about-us">
    <dl class="accordion"> <dt>
            <a href="#title">Title</a>
        </dt>

        <dd style="">
            <p>lorem upsum dolor sit amet</p>
        </dd> <dt>
            <a href="#title">Title</a>
        </dt>

        <dd style="">
            <p>lorem upsum dolor sit amet</p>
        </dd> <dt>
            <a href="#title">Title</a>
        </dt>

        <dd style="">
            <p>lorem upsum dolor sit amet</p>
        </dd> <dt>
            <a href="#title">Title</a>
        </dt>

        <dd style="">
            <p>lorem upsum dolor sit amet</p>
        </dd> <dt>
            <a href="#title">Title</a>
        </dt>

        <dd style="">
            <p>lorem upsum dolor sit amet</p>
        </dd>
    </dl>
</div>

Comments

0

There are much answers already.However you can try this code at thisfiddle http://jsfiddle.net/m0zehxpc/2/

var datapanel = $('.accordion > dd').hide(); 
$('.accordion > dt > a').click(function() {
 datapanel.slideUp(200);
 $(this).parent().next().slideDown();
 return false;
});

Comments

0
  $("dt").click(function () {     
  $('dd').slideUp();
  $(this).next('dd').slideDown();
 });

Comments

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.