views:

251

answers:

1

I have a html structure like :

<div onmouseover="enable_dropdown(1);" onmouseout="disable_dropdown(1);">

            My Groups <a href="#">(view all)</a>
            <ul>
                <li><strong>Group Name 1</strong></li>
                <li><strong>Longer Group Name 2</strong></li>
                <li><strong>Longer Group Name 3</strong></li>
            </ul>

            <hr />

            Featured Groups <a href="#">(view all)</a>
            <ul>
                <li><strong>Group Name 1</strong></li>
                <li><strong>Longer Group Name 2</strong></li>
                <li><strong>Longer Group Name 3</strong></li>
            </ul>

</div>

I want the onmouseout event to be triggered only from the main div, not the 'a' or 'ul' or 'li' tags within the div!

My onmouseout function is as follows :

function disable_dropdown(d)
{   
   document.getElementById(d).style.visibility = "hidden";
}

Can someone please tell me how I can stop the event from bubbling up? I tried the solutions (stopPropogation etc) provided on other sites, but I'm not sure how to implement them in this context.

Any help will be appreciated.

Thanks a lot!

A: 

The events that you really want to use are onmouseenter and onmouseleave, however they are not implemented in all browsers. You could look to implement them yourself, however you would in this case be better off using a library that has already solved the problem cross browser for you. So, in jQuery

<div id="main">

            My Groups <a href="#">(view all)</a>
            <ul>
                <li><strong>Group Name 1</strong></li>
                <li><strong>Longer Group Name 2</strong></li>
                <li><strong>Longer Group Name 3</strong></li>
            </ul>

            <hr />

            Featured Groups <a href="#">(view all)</a>
            <ul>
                <li><strong>Group Name 1</strong></li>
                <li><strong>Longer Group Name 2</strong></li>
                <li><strong>Longer Group Name 3</strong></li>
            </ul>

</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
     $('#main').hover(function() { enable_dropdown(1); },   // mouseenter
                      function() { disable_dropdown(1); }); // mouseleave
</script>
Russ Cam
Thanks Russ! Using Jquery made it so much simpler!
Kartik Rao
@Kartik - Glad to help. There are a lot of "just use jQuery" answers on stackoverflow to JavaScript questions that can easily be solved with plain JavaScript. This one, whilst could be solved relatively easily with plain JavaScript (would need to pass the event object to your event handler functions and then check the element that raised the event is the div in question and the cursor is not already within the boundaries of the div), jQuery is going to greatly simplify the code required for a solution.
Russ Cam