0

I have a link for menu profile. It has a class called 'user-button'. When user clicks on it, jquery adds another class to ul list to display it.

$(".user-button").click(function() {
    if(!$(".arrow").hasClass('toggle')) {
        $(".arrow").addClass('toggle');
        $(".profile-action").addClass('display-it');
    } else {
        $(".arrow").removeClass('toggle');
        $(".profile-action").removeClass('display-it');
    }
});

Image reference: http://cl.ly/0R1u3A0h341s2w3a3Y3L

How do i make user click on other element such as body to close '.profile-action'? eg: facebook modaless

Thank you.

1 Answer 1

1

Add this to your code:

$('html').click(function() {
     $(".arrow").removeClass('toggle');
     $(".profile-action").removeClass('display-it');
 });

And change your current code to prevent propgation:

$(".user-button").click(function(event) {
    event.stopPropagation();
    if(!$(".arrow").hasClass('toggle')) {
        $(".arrow").addClass('toggle');
        $(".profile-action").addClass('display-it');
    } else {
        $(".arrow").removeClass('toggle');
        $(".profile-action").removeClass('display-it');
    }
});
Sign up to request clarification or add additional context in comments.

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.