1

How do I change the text value from 'close' to 'open' or vice versa when clicking my H4 element?

http://jsfiddle.net/8snk7ev6/

$(document).ready(function($) {
    $('#accordion').find('.accordion-toggle').click(function(){

        //Expand or collapse this panel
        $(this).next().slideToggle('fast', function(){
            var status = $(this).is(':hidden') ? 'close' : 'open';
            $(this).next('.accordion-status').html(status);
        });

    });
});

2 Answers 2

1

According to your current DOM structure, the selector should be:

$(this).prev('h4').find('.accordion-status').html(status);

Because this refers to the .accordion-content div, and the accordion-status div you're looking for is actually before it. Also, it's wrapped with an h4 element.

See Fiddle

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

4 Comments

@michaelmcgurk : Welcome to stackoverflow. In this QA site, when an answer is realized, we do not append "(SOLVED)" to the question title. Instead, we mark a correct answer by clicking on the checkbox to the upper-left of that answer. If you come up with the answer yourself, then post your own answer, give it some time, and if no one has a better answer (your discretion), you can accept your own after a 48-hour wait period.
@CerlinBoss "member for 5 months" - I feel like I should be welcoming you to SO ;-) Apologies, the real world got in the way as it can do sometimes. If you'll have a look through my previous questions, you'll see I always mark a correct answer and give thanks. I never append "(SOLVED)" to the question title. Take care : )
the point of that comment is to make sure that the guy who put the effort get the credits for it. also being a member for more than 3 years doesn't justify anything. if you are really into stackoverflow then you should have accepted the answer before anyone pointing it out. FYI I am glad that you accepted the answer which helped you. :)
I'm glad too :-) Unfortunately, there was a limit of 9 minutes between that answer being posted & myself being able to accept it. In that time I was taken away from my computer on a personal errand and was unable to accept the answer until now. As you can see from my profile, I accept the vast majority of answers. This was a rare occasion where there was a delay : D
0

check below code or go to updated fiddle jsfiddle

$(document).ready(function($) {
    var OBJ = '';
    $('#accordion').find('.accordion-toggle').click(function(){
        OBJ = $(this);
        //Expand or collapse this panel
        $(this).next().slideToggle('fast', function(){
            var status = $(this).is(':hidden') ? 'close' : 'open';
            OBJ.find('div.accordion-status').html(status);
        });

    });
});

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.