Dino Prašo
Dino Prašo

Reputation: 611

jQuery click to toggle

I have this code in my script:

$(document).ready(function() {
    $('*').not('div#user').click(function() {
        $('div#userSettings').hide();
    });
    $('div#user').click(function() {
        $('div#userSettings').toggle();
        $('div#profileSettings').toggleClass('rotate');
    });
});

I need the div#userSettings to be hidden whenever anything but it's button or itself is clicked, and I want it to appear only when I click on the div#user.

the toggleclass does still work in this, just that the div#userSettings does not appear at all

Upvotes: 1

Views: 55

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388436

You can stop the event propagation

$(document).ready(function () {
    $(document).click(function (e) {
        $('#userSettings').hide();
    })
    $('#user').click(function (e) {
        e.stopPropagation();
        $('#userSettings').toggle();
        $('#profileSettings').toggleClass('rotate');
    });
    $('#userSettings').click(function (e) {
        e.stopPropagation();
    });
});

Demo: Fiddle

Upvotes: 3

Related Questions