Search code examples
jqueryhtmlcssbindunbind

jQuery deactivate click event on one div when another div is clicked


I'm currently making a website and I have 2 clickable divs "#coloursettings" and "#powersettings" both successfully unbind the menu ("#tab a") but I want each clickable div to deactivate the other, then re-activate it when clicked again. I've tried entering the corresponding ID into the same method that unbinds/binds the "#tab a" but it doesn't deactivate the other clickable div. Can anyone help make this work?

//Colour Settings Menu//
var clicked1 = true;
$("#coloursettings").click(function() {
    $("#colourselect").toggle();

    $(".content, .tab").toggleClass('blur');

    if (clicked1) {
        $("#tab a").unbind("click", foo);
        clicked1 = false;
    }
    else {
        $("#tab a").click(foo);
        clicked1 = true;
    }       
});

//Power Menu//
var clicked = true;
$("#powersettings").click(function() {
    $("#powermenu").toggle();

    $(".content, .tab").toggleClass('blur');

    if (clicked) {
        $("#tab a").unbind("click", foo);
        clicked = false;
    }
    else {
        $("#tab a").click(foo);
        clicked = true;
    }       
});

Relevant HTML

<div class="icon" id="coloursettings">...</div>
<div class="icon" id="powersettings">...</div>

Here's a link to my JSFiddle


Solution

  • I'm not sure if I understood your needs properly or not, still tried to help you . see the below link if your needs are satisfied or not..

    //Colour Settings Menu//
    var clicked1 = false;
    $("#coloursettings").click(function () {
        if (clicked) {
            return;
        }
        $("#colourselect").toggle();
        $(".content, .tab").toggleClass('blur');
        if (clicked1) {
            //$("#tab a").unbind("click", foo);
            clicked1 = false;
        } else {
            //$("#tab a").click(foo);
            clicked1 = true;
        }
    });
    //Power Menu//
    var clicked = false;
    $("#powersettings").click(function () {
        if (clicked1) {
            return;
        }
        $("#powermenu").toggle();
        $(".content, .tab").toggleClass('blur');
        if (clicked) {
            //$("#tab a").unbind("click", foo);
            clicked = false;
        } else {
            //$("#tab a").click(foo);
            clicked = true;
        }
    });
    //foo
    function foo(e) {
        if(clicked || clicked1)
        {
            return;
        }
        hideContentDivs();
        var tmp_div = $(this).parent().index();
        $('.main #content').eq(tmp_div).fadeIn(1000);
    }
    

    See This