Abdul Shakoor Kakar
Abdul Shakoor Kakar

Reputation: 611

Accordion active don't work on first load

when it loads first time i am trying to active first div but it makes all div active except of first. here it is on jsfiddle.

$(".accordion > span").click(function(){
  $('.accordion > span').removeClass('active');

  $(this).addClass('active');
    if(false == $(this).next().is(':visible')) {
        $('.accordion > div').slideUp(300);
    }
    $(this).next().slideToggle(300);
});

var animationIsOff = $.fx.off;
$.fx.off = true;
$('.accordion > span:eq(0)').click()
$.fx.off = animationIsOff;

here is HTML

<div class="accordion">
    <span>Accor 1</span>
    <div>
Content here
    </div>
</div>

<div class="accordion">
    <span>Accor 2</span>
    <div>
Content here
    </div>
</div>

<div class="accordion">
    <span>Accor 2</span>
    <div>
Content here
    </div>
</div>

any help will be highly appreciated.

Upvotes: 1

Views: 89

Answers (1)

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

You are very close.

All you need to do is add active class for first span.

$('.accordion > span:eq(0)').addClass('active');

http://jsfiddle.net/ym2E3/2/

OR

You can hide other div.

$('.accordion > span:not(:eq(0))').next('div').css('display','none');

http://jsfiddle.net/ym2E3/9/

Upvotes: 1

Related Questions