Kristian Kristian - 1 year ago 100
jQuery Question

how to close jQuery tooltip

I've been trying to make very simple javascript tooltip with jQuery but I've hit a brick wall. The idea is to have little inline element (

) inside a
. The
element will contain a tooltip
with a little html (image and link). Tooltip should be opened when clicked on the
element and closed when clicked outside of it or outside of the tooltip.

So far, opening the tooltip is not a problem but closing is.


#colors > div {
background-color: red;
height: 50px;
width: 50px;
margin: 5px;

#colors > div > span {
min-height: 10px !important;
min-width: 10px !important;
border: 3px solid black;
position: relative;

.tooltip {
border: 2px solid blue;
display: none;

<script src=""></script>
$(function () {
// generate boxes and tooltips
for (var i = 0; i < 9; i++) {
$('#colors').append('<div id="' + i + '"><span><div class="tooltip"><a href="#">add to favorites</a></div></span></div>');

$('#colors').delegate('span', 'click', function (event) {
$(this).children('.tooltip').css({position:'absolute', top:'5px', left:'5px'}).fadeIn();
// bottom one won't work

$(document).delegate('body', 'click', function (event) {
var that = this
$.each($('.tooltip'), function (index, element) {
// it's always visible ...
//if ($(element).is(':visible')) {

// doesn't work either
if ($(element).is(':visible') && $(element).has( === 0) {
var s =;

console.log([($(s) ==,, index, element, $(element).has(, that]);
<div id="colors"></div>

I can't seem to find a way to close the tooltip if click is outside of the
and tooltip.

Answer Source

Something like this should work fine :)

 $(document).mouseup(function (e)
     var container = $("YOUR CONTAINER SELECTOR");

     if (container.has( === 0)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download