michaelmcgurk
michaelmcgurk

Reputation: 6519

Change text value on click with jQuery

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

    });
});

Upvotes: 1

Views: 88

Answers (2)

Nishit Maheta
Nishit Maheta

Reputation: 6031

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

    });
});

Upvotes: 0

haim770
haim770

Reputation: 49123

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

Upvotes: 1

Related Questions