lharby
lharby

Reputation: 3265

swapping title for jQuery accordion if accordion is opened or closed

I am 95% finished with this (I hope). I am working on an accordion function that changes the text in the head of the accordion depending if the accordion is opened or closed. (I've got a plus and minus image working).

At present if the client clicks on the accordion to open and close it the title changes correctly, but if the client opens a different accordion the original accordion will slide up, but the title is not updated. I've tried a couple of options I thought would work, but no success as yet.

I've made a jsfiddle here

jquery is:

 $(document).ready(function() {
// accordion functionality 
$('.accordion-head').click(function(e){
    var content = $(this).parent().find('.accordion-content');
    var head = $(this).parent().find('.accordion-head');
    var $showMore = "Show me more";
    var $hideContent = "Hide this offer";            

    content.addClass('actual');

    $('.accordion-content').not('.actual').each(function(){
        if ($(this).hasClass('accordion-opened')){
            $(this).slideUp(200,function(){
                $(this).toggleClass('accordion-opened');
                $(this).parent().find('.accordion-head').toggleClass('accordion-open');
            });
        }
    });

    $(this).toggleClass('accordion-open');
    content.removeClass('actual');               

    if($(this).hasClass('accordion-open')){
        content.slideDown(200,function(){
            content.toggleClass('accordion-opened');
            head.html($hideContent);
        });
    }else{
        content.slideUp(200,function(){
            content.toggleClass('accordion-opened');
            head.html($showMore);
        });
    }
    e.preventDefault();
   });

 });​

HTML looks like this:

  <div class="accordion">
         <div class="accordion-head">
                        <a href="#">
                           <span class="accordion-heading">Show me more</span>
                        </a>
                    </div>
                    <div class="accordion-content">
                        Including Thames Water, Severn Trent, Anglian Water, Yorkshire Water and many more, plus all council tax local authorities. Cashback on mortgage payments up to maximum monthly mortgage payment of £1,000.
                    </div>
                </div>


                <span class="spacer"></span>

                <div class="accordion">
                    <div class="accordion-head">
                        <a href="#">
                            <span class="accordion-heading">Show me more</span>
                        </a>
                    </div>
                    <div class="accordion-content">
                        Including British Gas, SSE, EDF Energy, E.ON, npower and many more.
                    </div>
                </div>
                <span class="spacer"></span>
            etc.

Hope someone can help.

Upvotes: 2

Views: 1101

Answers (2)

Nandakumar V
Nandakumar V

Reputation: 4625

You can also insert $('.accordion-head').html($showMore); inside click function , which will update all the headers intially to show more and then update the individual headers according to the conditions.
var $showMore = "Show me more";
var $hideContent = "Hide this offer";
$('.accordion-head').html($showMore);
content.addClass('actual');

Upvotes: 1

Alex K.
Alex K.

Reputation: 175866

You could rewrite the header during the collapse loop by removing head.html($showMore) & changing

$(this).parent().find('.accordion-head').toggleClass('accordion-open');

to

$(this).parent().find('.accordion-head').toggleClass('accordion-open').html($showMore);

Upvotes: 2

Related Questions