Solved

Javascript If click outside layer

Posted on 2007-11-14
3
960 Views
Last Modified: 2008-02-01
Hi Experts

I have a div layer that appears onclick.

My question is that to hide the layer I have a X with a onclick style.display='none'

How can i make it so that if I click anywhere on the screen that is not in the layer, the layer hides?

Cheers
0
Comment
Question by:nikdonovanau
  • 2
3 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20278515
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 20278540
So with a few changes to the code  from the link

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<style type="text/css">
#mydiv{
    background-color: #999999;
    height: 100px;
    width: 100px;
    display:none;
    border:1px solid black
}
</style>
<script type="text/javascript">
document.onclick=check;
function check(e){
  var target = (e && e.target) || (event && event.srcElement);
  var obj = document.getElementById('mydiv');
  var obj2 = document.getElementById('sho');
  var parent = checkParent(target);
  if(parent){obj.style.display='none'}
  if(target==obj2){obj.style.display='block'}
}
function checkParent(t){
  while(t.parentNode){
    if(t==document.getElementById('mydiv')){
      return false
    }
    t=t.parentNode
  }
  return true
}
</script>
</head>
<body>
<a href="#" id="sho">show the div</a>
<div id="mydiv">
<div style="float:right"><a href="#" onClick="document.getElementById('mydiv').style.display='none';return false">[X]</div>
</div>
</body>
</html>
0
 
LVL 1

Author Comment

by:nikdonovanau
ID: 20278636
Thanks for that :)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question