Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
-4
rated 0 times [  0] [ 4]  / answers: 1 / hits: 127693  / 14 Years ago, fri, july 23, 2010, 12:00:00
$(document).click(function(evt) {
var target = evt.currentTarget;
var inside = $(.menuWraper);
if (target != inside) {
alert(bleep);
}

});


I am trying to figure out how to make it so that if a user clicks outside of a certain div (menuWraper), it triggers an event.. I realized I can just make every click fire an event, then check if the clicked currentTarget is same as the object selected from $(.menuWraper). However, this doesn't work, currentTarget is HTML object(?) and $(.menuWraper) is Object object? I am very confused.


More From » jquery

 Answers
11

Just have your menuWraper element call event.stopPropagation() so that its click event doesn't bubble up to the document.



Try it out: http://jsfiddle.net/Py7Mu/



$(document).click(function() {
alert('clicked outside');
});

$(.menuWraper).click(function(event) {
alert('clicked inside');
event.stopPropagation();
});




Alternatively, you could return false; instead of using event.stopPropagation();


[#96138] Tuesday, July 20, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aubreeg

Total Points: 437
Total Questions: 102
Total Answers: 102

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
;