/    Sign up×
Community /Pin to ProfileBookmark

Closing a floating div layer

I’m trying to make a floating div. Actually, something very similar to the “Search” floating div on this forum. Anyway, I want to make it so when the user clicks somewhere outside of the search div, it closes itself (again, like the search box on this forum does). Does anyone know how I can detect a click that occurs anywhere outside of the search box?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisMay 14.2007 — You might have success using onblur().
Copy linkTweet thisAlerts:
@dmeglioauthorMay 14.2007 — onblur doesn't trigger for div's. According to the HTML 4 spec, it only triggers for, A, AREA, LABEL, INPUT, SELECT, TEXTAREA, and BUTTON. IE triggers it for a DIV, but not Firefox.
Copy linkTweet thisAlerts:
@Orc_ScorcherMay 14.2007 — Register a click event listener for the document that checks whether the click occured in your div. Quick & dirty example<div id="box"
style="height: 3em; position:absolute; top: 20%; left: 15%; border: 3px double">
<p>Click anywhere outside this box to close it.
</div>
<script>

document.onclick = function (e) {
e = e || event
var target = e.target || e.srcElement
var box = document.getElementById("box")
do {
if (box == target) {
// Click occured inside the box, do nothing.
return
}
target = target.parentNode
} while (target)
// Click was outside the box, hide it.
box.style.display = "none"
}
</script>
Copy linkTweet thisAlerts:
@uberzevMar 01.2008 — Register a click event listener for the document that checks whether the click occured in your div. Quick & dirty example<div id="box"
style="height: 3em; position:absolute; top: 20%; left: 15%; border: 3px double">
<p>Click anywhere outside this box to close it.
</div>
<script>

document.onclick = function (e) {
e = e || event
var target = e.target || e.srcElement
var box = document.getElementById("box")
do {
if (box == target) {
// Click occured inside the box, do nothing.
return
}
target = target.parentNode
} while (target)
// Click was outside the box, hide it.
box.style.display = "none"
}
</script>
[/QUOTE]

Great script Orc, any chance of getting a version that has a link to re-open the box?
×

Success!

Help @dmeglio spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...