Help - Search - Members - Calendar
Full Version: Div/Javascript Problem :(
Invision Power Services > Community Forums > Community Web Design and Coding
Danny
I've written some javascript that allows me to display/hide divs whenever you click certain links. Now, to do this, I have the div say that the display="none". But, now I want to set it so that whenever you mouseout from it, it closes. It does that, except whenever the mouse hovers over any div that is inside of the div it closes too. I don't want this. Is there some CSS that can stop this? Here is the HTML:

(click this and it displays it)
CODE
<a href="#" onclick="javascript:toggle_box('openb1');">Test</a>


(the code it displays)
CODE
<div id="openb1" class="box" style="display:none;" onmouseout="this.style.display='none';">
         <div class="boxrow">Testing1</div>  
         <div class="boxrow">Testing2</div>
         <div class="boxrow">Testing3</div>
         <div class="boxrow">Testing4</div>
     </div>


So, anything inside of the "openb1" that is in a div or span causes it to close when it is mouse overed. Basically, whenever the mouse hovers over "boxrow" it thinks it is outside of "openb1" and shuts itself. Can someone tell me how to stop this? original.gif
Michael K.
Whoa.. I'm having the exact (or well, similar at least) problem. And I can't find a simple solution either. Somehow we need to have it ignore those child elements. ermm.gif
Nash12
Have a look at an example I made. Is it that how you want it to work?

If so, just insert this code in the header of your website:

CODE
  <script type="text/javascript">
  function foo() {
    var divs = document.getElementsByTagName("div");
    var klasse = "boxrow"; // the class of the sub-divs
    
    for(i=0; i<divs.length; i++) {
      if(divs[i].className == klasse) {
        // mouseover
        divs[i].onmouseover = function() {
        this.parentNode.style.display = "block";  
        }
        // mouseout
        divs[i].onmouseout = function() {
        var id = this.parentNode.id;
        document.getElementById(id).style.display = "none";
        }
      }
    }
  }
  
  window.onload = foo;
  </script>
Danny
Thanks! I PM'ed you with a question. original.gif
Trel
I would suggest delaying the closing by about .5 seconds to allow for normal mouse movement.
Danny
QUOTE(Trel @ May 6 2005, 06:12 PM) *
I would suggest delaying the closing by about .5 seconds to allow for normal mouse movement.


How do you do that? original.gif

Also, just incase I want to do this, how do you do it? How can you get it so that when you click outside of the DIV, then it closes (as IPB does) original.gif
Trel
I think that would be a focus related event, I think onBlur is the one you want.
(don't take that as gospel though)
Nash12
QUOTE(Danny @ May 6 2005, 11:57 PM) *
How do you do that? original.gif


You would make use of the "setTimeout" function. I set up a New Demo, the only difference is the updated script. original.gif


QUOTE(Trel @ May 7 2005, 04:52 AM) *
I think that would be a focus related event, I think onBlur is the one you want.
(don't take that as gospel though)


That would be starting point, yes. You check if the opener-link is focused. If so, then show the div. As soon as you click somewhere, the focused opener link "blur"īs and the div gets closed.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.