views:

126

answers:

1

Hi all!

My problem: For example: I have a link, when I click on it a popup div is shown, and when I click on the link again then closes the popup, that's ok, but how can I do to close the opened popup box when I click anywhere else outside of the popup.

I've searched a lot and I found some solutions, but those weren't enough good to me, as I know there is a way to hide the box by offset X, and offset Y, as I understood works in this way, the javascript tracks the position of the popup and if the users clicks on an other position it closes or what..

Please could someone tell me how can I do it this way? It's really important for me!!

Please if you don't understand something, or do you have any questions write here and I will try to explain it better!

Thanks, Adam

+1  A: 

Csirkepöcsű? :)

You need an onclick handler for the document, or the body.

​See it in action.

// make sure that click on the popup does nothing
document.getElementById("pop").onclick = function(e) {
  // in all browsers
  if(e && e.stopPropagation) {
    e.stopPropagation();
  } else {
    e = window.event;
    e.cancelBubble = true;
  }
};

// click anywhere else
document.documentElement.onclick = function() {
  // will hide the popup
  document.getElementById("pop").style.display = "none";
};
galambalazs
galambalazs, I don't realy know what do you want with this :)) but I was asking for something else :S Don't you understood something?the main role is to:"close the opened popup box when I click anywhere else outside of the popup." with cordinates
CIRK
you don't need the coordinates.
galambalazs
check the example link (updated). Who the hell gave -1?
galambalazs
Thanks galambalazs I will try it ;) I don't know who was given you a -1
CIRK
Hmm, I rewrited it and works perfectly, but I can't use it, as I see you are an advanced programmer, I'm just a beginner with Javascript, I don't really understand what are you doing here, if you could explain that would be great, for example what is the 'e', stopPropagation, cancelBubble.And I want a function what I can use anywhere. And are you hungarian? Balazs is a hungarian name :D I'm hungarian too and as I see you can write in hungarian toooo :)) (Ha magyar vagy orulnek ha magyarul elmagyaraznad hogy ez pontosan mi is )
CIRK
And something else, I want to use this in popups, not the windowed one just div popups, and if I have more than one popups then I want to close the last opened one. So for example: I have 2 links If I click on the first shows the first popup then I click on the second link then the first popup needs to hide and show the second popup and so on..
CIRK
I've made this by using a variable called lastID, does the same what I said before, but I don't know if this is the best way to do it, and I don't really know how to use it with your code.
CIRK
I will show it. (holnap) ;)
galambalazs
Hát azt nagyon megköszönném! Örülök hogy vannak jó magyar programozók annak meg mégjobban hogy segítenek másokon is! Amúgy azért van ilyen buta email címem mert ezt használom regisztrálásokhoz, sokat spammelnek így könnyű védekezni :)
CIRK
sent you a mail. let's not spam here :)
galambalazs