<p>I'm using JavaScript to hide an image and show some text thats hidden under it. But, when the text is shown if you scroll over it, it fires the mouseout event on the container, that then hides the text and shows the image again, and it just goes into a weird loop.</p>

<p>The html looks like this:</p>

<pre><code>&lt;div onmouseover="jsHoverIn('1')"
     onmouseout="jsHoverOut('1')"&gt;
    &lt;div id="image1" /&gt;
    &lt;div id="text1" style="display: none;"&gt;
        &lt;p&gt;some content&lt;/p&gt;
        &lt;p&gt;some more content&lt;/p&gt;
    &lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>And the javascript (It uses scriptaculous):</p>

<pre><code>function jsHoverIn(id) {
  if(!visible[id]) {
    new Effect.Fade ("image" + id, {queue: { position: 'end', scope: id } });
    new Effect.Appear ("text" + id, {queue: { position: 'end', scope: id } });
    visible[id] = true;
  }
}
function jsHoverOut (id) {
  var scope = Effect.Queues.get(id);
  scope.each(function(effect) { effect.cancel(); });

  new Effect.Fade ("text" + id, {queue: { position: 'end', scope: id } });
  new Effect.Appear ("image" + id, {queue: { position: 'end', scope: id } });
  visible[id] = false;
}
</code></pre>

<p>This seems really simple, but i just cant wrap my head around it.</p>

[{'answer_id': 49645, 'author': 'Ryan Lanciaux', 'author_id': 1385358, 'author_profile': 'https://Stackoverflow.com/users/1385358', 'pm_score': -1, 'selected': False, 'text': '<p>This may not be the best solution but you could set a global boolean variable that would be accessible to both methods that would just specify if the last action was HoverIn or HoverOut. You could use this boolean variable to determine if the code should run or not. </p>\n\n<pre><code>if (bWasHoverIn){\n   ...\n}\n</code></pre>\n'}, {'answer_id': 49675, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Shouldn't the onmouseover event be on the image div and the onmouseout event be on the text div?</p>\n"}, {'answer_id': 49687, 'author': 'SpoonMeiser', 'author_id': 1577190, 'author_profile': 'https://Stackoverflow.com/users/1577190', 'pm_score': 4, 'selected': True, 'text': "<p>I'd give the container div:</p>\n\n<pre><code>position: relative;\n</code></pre>\n\n<p>and add a third div in the container (should be the last child of the container) with:</p>\n\n<pre><code>position: absolute;\ntop: 0;\nbottom: 0;\nleft: 0;\nright: 0;\n</code></pre>\n\n<p>and catch the mouseover and mouseout events on this div instead.</p>\n\n<p>Because it has no child elements, you shouldn't get spurious mouseover and mouseout events propagating to it.</p>\n\n<p><em>Edit:</em></p>\n\n<p>What I believe happens, is that when the cursor moves from a parent element onto a child element, a mouseout event occurs on the parent element, and a mouseover event occurs on the child element. However, if the mouseover handler on the child element does not catch the event and stop it propagating, the parent element will also receive the mouseover event.</p>\n"}, {'answer_id': 49698, 'author': 'pkaeding', 'author_id': 4257, 'author_profile': 'https://Stackoverflow.com/users/4257', 'pm_score': 0, 'selected': False, 'text': "<p>I'm not sure if this would fit with the rest of your styling, but perhaps if you changed the css on the text div so it was the same size as the image, or fixed the size of the outer div, then when the mouseover event fired, the size of the outer div wouldn't change so much as to cause the mouseout event.</p>\n\n<p>Does this make sense?</p>\n"}, {'answer_id': 196838, 'author': 'eyelidlessness', 'author_id': 17964, 'author_profile': 'https://Stackoverflow.com/users/17964', 'pm_score': 2, 'selected': False, 'text': '<p>It sounds like what you really want is <code>mouseenter</code>/<code>mouseleave</code> (IE proprietary events, but easy to emulate):</p>\n\n<pre><code>// Observe mouseEnterLeave on mouseover/mouseout\nvar mouseEnterLeave = function(e) {\n    var rel = e.relatedTarget, cur = e.currentTarget;\n    if (rel &amp;&amp; rel.nodeType == 3) {\n        rel = rel.parentNode;\n    }\n    if(\n        // Outside window\n        rel == undefined ||\n        // Firefox/other XUL app chrome\n        (rel.tagName &amp;&amp; rel.tagName.match(/^xul\\:/i)) ||\n        // Some external element\n        (rel &amp;&amp; rel != cur &amp;&amp; rel.descendantOf &amp;&amp; !rel.descendantOf(cur))\n    ) {\n        e.currentTarget.fire(\'mouse:\' + this, e);\n        return;\n    }\n};\n$(yourDiv).observe(\'mouseover\', mouseEnterLeave.bind(\'enter\'));\n$(yourDiv).observe(\'mouseout\', mouseEnterLeave.bind(\'leave\'));\n\n// Use mouse:enter and mouse:leave for your events\n$(yourDiv).observe(!!Prototype.Browser.IE ? \'mouseenter\' : \'mouse:enter\', yourObserver);\n$(yourDiv).observe(!!Prototype.Browser.IE ? \'mouseleave\' : \'mouse:leave\', yourObserver);\n</code></pre>\n\n<p>Alternatively, <a href="http://dev.rubyonrails.org/attachment/ticket/8354/event_mouseenter_106rc1.patch" rel="nofollow noreferrer">patch prototype.js</a> and use <code>mouseenter</code> and <code>mouseleave</code> with confidence. Note that I\'ve expanded the check for leaving the window or entering XUL chrome; this seemed to fix some edge cases in Firefox for me.</p>\n'}, {'answer_id': 57263910, 'author': 'Jayanth G', 'author_id': 10423590, 'author_profile': 'https://Stackoverflow.com/users/10423590', 'pm_score': -1, 'selected': False, 'text': '<p>Try using \nonmouseenter instead of onmouseover and\nonmouseleave instead of onmouseout.</p>\n'}]
['https://Stackoverflow.com/questions/49630', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/550/']