views:

33

answers:

1

Hi to all,

When i click on button,one div is showing..whenever i click on the body,that div will be hide.

How can we achieve this functionality .. i write one code is as below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
function HideSubMenus(e)
{
 var targ;
 if (!e) var e = window.event;
 if (e.target) targ = e.target;
 else if (e.srcElement) targ = e.srcElement;
 if (targ.nodeType == 3) // defeat Safari bug
  targ = targ.parentNode;
 for (var i=0; i!=5; i++) {
  if (!targ) {
   break;
  } else if (targ.className=="divclass") {
   return;
  }
  targ = targ.parentNode;
 }

 if(document.getElementById("showdivid"))
 {
  document.getElementById("showdivid").style.display='';
 }
}
</script>
</head>

<body onclick="HideSubMenus(event);">
<input type="button" name="button" value="Click ME" onclick="document.getElementById('showdivid').style.display='';" />
<div id="showdivid" class="divclass" style="display:none;">
TeSt DIV
</div>
</body>
</html>

regards raj

+1  A: 

Hope this helps.

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
function HideSubMenus(e)
{
 var targ;
 if (!e) var e = window.event;
 if (e.target) targ = e.target;
 else if (e.srcElement) targ = e.srcElement;
 if (targ.nodeType == 3) // defeat Safari bug
  targ = targ.parentNode;
  if(targ.className==='btn'){
    document.getElementById('showdivid').style.display='';
    return;
  }else{
    document.getElementById('showdivid').style.display='none';
  }
}

</script>
</head>

<body onclick="HideSubMenus(event);">
<input type="button" name="button" class='btn' value="Click ME" />
<div id="showdivid" class="divclass" style="display:none;">
TeSt DIV
</div>
</body>
</html>
Fopfong
Hey It is working fine.thank you very much