Hey there. The situation is like this:

  1. The user clicks a button.
  2. Tooltip pops up.
  3. The user clicks the button again OR the user clicks anywhere outside the tooltip.
  4. The tooltip is removed.

Now I can get either of the events described in step 3 to work, but I can't get them to work at the same time. My code is in short like this (I'm not copying all code, just explaining it, so not all functions that I mention are real functions as you will see ^^):

button onclick="function() // Checks if tooltip exists and if yes, removes it. If not, creates it.
(works, no need to show you the code I think).





$(window).click(function()
{
 if(tooltip exists)
  if(clicked outside the tooltip)
   remove tooltip
});

Now the problem is (I think) that when the button is clicked the onclick="" is executed BEFORE the window.click function is executed, as the tooltip appears to exist (and is removed!) when the window.click function checks for its presence as soon as I click the button. When I change the window.click function to window.mouseup, the tooltip does not get removed when I click the button, but it does when I click outside the element, which is great. Only problem is that the tooltip now does not get removed anymore when I click the button again.. Help please!

Recommended Answers

All 4 Replies

You can make a simple test to verify what occurs.

Declare a var buttonClicked = false. Inside the $(button).click() set it as true, then inside the $(window).click() alert the var to see it's state.

If the button is 'clicked' before, you can use this var to ignore the event at the first window.click().

Yea I tried that, and somehow it is set to "true", that's why I figured the button's onclick event is handled before the window.click event :). Thanks for your input though! Any other clues on how I could get this to work?

So, if you have the var setted to true, you can ignore the window.click. And after ignoring the window.click, set the var to false, so that in the next click you can close it.

Doesn't that work? Any doubts in this implementation?

Sorry for the late response, was on a trip to Dublin :). Thanks for the reply, that does sound like a solution. Can't remember 100% accurately what the problem was at the current moment (as I've been away for a couple of days) but I'll surely check it out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.