1

I have a jQuery function that will change the CSS of the .flavor box with toggleClass and also add some text to the bottom. I want to change this so that when I click my box again (it is already active), the .active-text disappears also. How can I do this? Thanks!

$(".flavor").click(function(event) {
    $(this).toggleClass("flavor-active");
    $(this).find('.active-text').css('visibility', 'visible');
});

2 Answers 2

1

You can just check for the class you're toggling

$(".flavor").click(function(event) {
    $(this).toggleClass("flavor-active")
           .find('.active-text') 
           .css('visibility', $(this).hasClass('flavor-active') ? 'visible' : 'hidden');
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this with .toggle()

$(this).find('.active-text').toggle()

Alternatively, if .active-text is a child of .flavor you can use CSS like this:

.flavor .active-text { display:none; }
.flavor-active .active-text { display:block; /* or inline, just not hidden */ }

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.