Click on not specified element

1.64K views
no comments
20 Sep 2015 1:34 am

Its really sometimes we need to call two functions, first we user click on specified element and another when user clicked on not specified element,

Eg: you have a textbox and control panel div of text box, but you want to call a function to show control div of  textbox when user clicked on specified text box but you also want to hide it when user clicked on not textbox.

something like in #jQuery

$("not:abc").click(function (){
    //not working…
})

I have wrote a solution for that, copy to use in your projects

//i prefer to use J instead $ or jQuery;
var j = jQuery;
j(document).ready(function () {
    var is_specified_clicked;
    j(".specified_element").click(function () {
        is_specified_clicked = true;
        setTimeout(function () {
            is_specified_clicked = false;
        }, 200);
    })
    j("*").click(function () {
        if (is_specified_clicked == true) {
//WRITE CODE HERE FOR CLICKED ON OTHER ELEMENTS
            j(".event_result").text("you were clicked on specified element");
        } else {
//WRITE CODE HERE FOR SPECIFIED ELEMENT CLICKED
            j(".event_result").text("you were clicked not on specified element");
        }
    })
})

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>