Forum Moderators: open

Message Too Old, No Replies

hide div onblur event

how to hide a div if we click somewhere else on the page

         

vsgill

8:29 pm on Jul 15, 2007 (gmt 0)

10+ Year Member



Hello Friends
I am new with java script and don't know if what i am trying to do is even possible..
I have a hidden div which shows up when clicked on a link. i want to hide the div if i click anywhere else on the page.
I am trying to use onblur event but it doest seem to work for some reason.
Can anyone look at the page below and suggest something.
thanks
Vik


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script language="javascript" type="text/javascript">
<!--
function toggleDisplay(id){
var me = document.getElementById(id);
if (me.style.display=="none"){
me.style.display="block";
me.focus();
}
else if (me.style.display=="block"){
me.style.display="none";
}else{
me.style.display="block";
me.focus();

}
}
// -->
</script>
</head>
<body>
<div id="storeSelectorDiv"> <a href="#" onclick="javascript:toggleDisplay('storeListDiv'); return false" class="listbox">Click here</a>
<!--Div for store selection-->
<div style="background-color:#CCCCCC; display:none; width:300px;" onblur="javascript:toggleDisplay('storeListDiv');" id="storeListDiv"> <br / >
Hide this box if we click anywhere else on the page. </div>
<br / >
<br / >
<input type="text" name="textfield" id="textfield" />
</div>
</body>
</html>

penders

11:36 am on Jul 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i want to hide the div if i click anywhere else on the page.

What about assigning it to the onclick event handler of the BODY element?


<body onclick="toggleDisplay('storeListDiv');">

zaxnyd

7:50 pm on Aug 3, 2007 (gmt 0)

10+ Year Member




What about assigning it to the onclick event handler of the BODY element?

<body onclick="toggleDisplay('storeListDiv');">

Body.conclick is a solution, but a less-than-ideal one. What if you had other body onclick events? You don't anymore. Onblur is the ideal way to handle it, and, according to microsoft, is supported for div tags.

[msdn2.microsoft.com...]

However, the functionality is spotty. According to my testing, a DIV's onblur only fires if you hit the tab key. Clicking elsewhere doesn't cut it.

Lame.

If anyone has a better alternative, I'd love to hear it. Still working on one myself.

syktek

8:42 pm on Aug 6, 2007 (gmt 0)

10+ Year Member



got this from the mozilla DOM reference:

onblur

Oppsite to MSIE, which almost all kinds of elements receive blur event, almost all kinds of elements on gecko browsers do NOT work with this event.