0

Im trying to build a collase expand function on my website.

I have the following that toggle ths hidden div only im trying to change the 'Expand' text to collapse, and then if clicked again, collapse > expand.

Can anybvody see what im doing wrong?

$('.coverage .expand').click(function(){
    $(this).parent().parent().find('.subitems').toggle('slow', function() {
        var togtit = $(this).parent().find('.expand');
        if((togtit).is('Expand')){
            togtit.html('Collapse')
        } else {
            togtit.html('Expand')
            }
    });
});

2 Answers 2

1

The problem is that you use .is() method in wrong way. You may try the following instead:

$(this).parent().find(".expand").html(function(i, html) {
    return html === "Expand" ? "Collapse" : "Expand";
});
Sign up to request clarification or add additional context in comments.

Comments

1
if(togtit.html() == 'Expand'){
     togtit.html('Collapse')
} else {
     togtit.html('Expand')
}

Above should work.

Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.

jQuery.is should be used to check if element matches selector. It will not test inner content of an element.

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.