Stewie Griffin
Stewie Griffin

Reputation: 9317

Bind js event to document except 1 element

Basically I have a popup and want to hide it when it is clicked anywhere outside, but one element (which is another popup, let say div#AnotherPopup). Then I click on #AnotherPopup, it should not hide the original one.. It does now, because I have this:

  $(document).bind('click', methods.hide);

How to bind it to document, except #AnotherPopup ? :)

Upvotes: 2

Views: 386

Answers (3)

Joe
Joe

Reputation: 82584

The click handler for #AnotherPopup is to stop document from getting a click event.

$(document).bind("click", function () {
   alert(this.innerHTML) 
});
$('#AnotherPopup').click(function () {return false;});

Example

Upvotes: 0

Artog
Artog

Reputation: 1142

By using unbind you can do this:

$(document).bind('click',methods.hide);
$('#AnotherPopup').unbind('click',methods.hide);

Read more about unbind here

Upvotes: -1

JaredMcAteer
JaredMcAteer

Reputation: 22536

You can't really but you can check inside methods.hide if the target element is #AnotherPopup and immediately return out of the function before doing anything.

Upvotes: 2

Related Questions