views:

29

answers:

3

I am using:

    Event.observe(window, 'load', function() {  
$$('li').invoke('observe', 'mouseover', function(event) {
    this.children[0].toggle();
});

$$('li').invoke('observe', 'mouseout', function(event) {
    document.children[0].toggle();
});
    });   

<ul>
  <li>
    <div style="display:hidden;">Hidden Div</div>
    <div>More content that isn't hidden</div>
  </li>
</ul>

To display a hidden div within an element when that element is moused over. It is partially working, however my code looks like the following:

When I rollover the li it displays the hidden div, however if I rollover the second div it hides the comment again, even though this div is in the li. Why? I found this questions, but it doesn't seem to work either for this context.

+1  A: 

No need to use prototype or javascript for this, use css

li.item:hover div.entry {
 display:block
}

li.item div.entry {
 display:none
}

you don:t use any css classes in your example code, but I would strongly recommend you do so for this case

<ul>
  <li class="item">
    <div class="entry">Hidden Div</div>
    <div>More content that isn't hidden</div>
  </li>
</ul>
Jonathan Fingland
li:hover will not work in IE6.
Josiah Ruddell
you're right, it won't. Which is another reason why unless you have an extremely compelling reason to do otherwise, you should cease supporting IE6. A not-insignificant proportion of web users disable javascript or do not have it available for accessibility reasons.
Jonathan Fingland
A: 

Add a class to the div that you want to perform the toggle on. Then inside the event handler use this.select() to find children with that class name.

 <ul>
  <li>
    <div class="hidden">Hidden Div</div>
    <div>More content that isn't hidden</div>
  </li>
</ul>

Event.observe(window, 'load', function() {  
    var lis = $$('li');

    var toggleFunc = function(event) {
        this.select('.hidden').toggle();
    }

    lis.observe('mouseover', toggleFunc);
    lis.observe('mouseout', toggleFunc);
});
Josiah Ruddell
mouseover and mouseout are not reliable enough for a toggle function. use addClassName() and removeClassName() instead. it's simply too easy to leave the item without triggering the mouseout (e.g. at the window edge).
Jonathan Fingland
if you are adding and removing classes won't you still have to have mouseover and mouseout triggers to know when to add and remove the classes? That's all I can tell from the documentation.
Ryan Max
@Ryan yes, but while you might have it erroneously stay open/closed once, it won't result in toggling to the wrong state on the next valid mouseover/mouseout. e.g. it avoids this scenario: valid mouseover -> toggle to unhidden. invalid mouseout -> no change. mouse re-enters browser window and then performs a valid mouseover -> toggle to hidden. And now you have the opposite of expected behaviour. mouseover hiding, and mouseout unhiding due to the toggle state.
Jonathan Fingland
A: 

A quick solution would be to use this plugin which adds "mouseEnter" and "mouseLeave" functionality for non-IE browsers.

clockworkgeek