0

Below is the code that I want to execute a command if the click is not on the rl_menu / any of it's child elements (.comment, .rl_arrow, etc)

TBR.mouse_down = function(e){
    target = $j(this)
    if (target != $j("#rl_menu") && target != $j("#rl_menu .comment") &&
    target != $j("#rl_menu .change") && target != $j("#rl_menu .rl_arrow_border") &&
    target != $j("#rl_menu .rl_arrow")){
        TBR.Menu.hide(); // this needs to be triggered when the menu is not clicked on.
    }
}

but when I inspect target, target is the document.

To recap:

Anywhere in the document, when the mouse is pressed, TBR.Menu.hide() should be executed. but if the click is within #rl_menu, TBR.Menu.hide() shouldn't be executed.

Am I going about this the wrong way?

user229044
  • 232,980
  • 40
  • 330
  • 338
NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352

2 Answers2

3

You can write this down a bit easyer?

$(function(){

    $("body").click(function(e){
        var t = $(e.target);
        if( !t.is("#rl_menu") && t.closest("#rl_menu").length == 0)
        {
            alert("CLICKD OUTSIDE THE GREEN")
        }
    });
});

Live demo: http://jsfiddle.net/JBuJm/

Niels
  • 48,601
  • 4
  • 62
  • 81
0

If you events bubble up the event.target will point to the element event is bound to. To get clicked element use event.originalEvent.target.

Litek
  • 4,888
  • 1
  • 24
  • 28