Don't run second function if first function is ran
javascript, jquery
Solution
Just check if the target is the `.trigger` or a parent (just in case) like you do in the if :
$(document).mouseup(function (e) {
if($(e.target).closest('.trigger').length) return;
var container = $('#container');
if (!container.is(e.target)) {
container.hide();
}
});
http://jsfiddle.net/Hx65Q/1034/
The best would be to rework event handler and use `click` on both element, not `mouseup` on one and `click` on the other. Then you can use stop propagation :
$("a.trigger").click(function (event) {
$('#container').slideToggle(100)
event.preventDefault();
event.stopPropagation(); //Stop the event from bubbling
});
$(document).click(function (e) {
var container = $('#container');
if (!container.is(e.target)) {
container.hide();
}
});
http://jsfiddle.net/Hx65Q/1036/
Problem
I have a header with a drop down, div, notification box (`#container`) that gets slide toggled. The JavaScript for the drop down box is: ``` $("a.trigger").click(function (event) { $('#container').slideToggle(100) event.preventDefault(); }); ``` I also have a function that closes the div box if I click anywhere outside of the `#container`: ``` $(document).mouseup(function (e) { var container = $('#container'); if (!container.is(e.target)) { container.hide(); } }); ``` When I click on the `a.trigger` it drops down the `#container` and then if I click anywhere outside the `#container` it properly closes it. But when I click on `a.trigger` while the `#container` is already exposed, it closes it from the second javascript function since `a.trigger` is outside of `#container` but then the first function gets triggered too afterwards and the `#container` slidestoggles down again right after being hidden. Here is a fiddle: http://jsfiddle.net/Hx65Q/1013/ I was thinking of making it so that if the first javascript function is called then it will just stop the second one from initiating. How would I approach this? and is there a more conventional approach?