[Javascript] Comparing objects

Bill Moseley moseley at hank.org
Tue May 16 12:53:43 CDT 2006


Maybe this is my lack of understanding of "this".

I have a popup menu system where there's always a menu open.
The menu closes when a new menu opens.

So I need to track what is the current open menu is so it can be
closed when mouseover another item.

Currently, I'm storing the current element in a global.

When I mouseover I need to test if I'm over an already open menu, and
just ignore that event.

    if ( this == current_open )
        return;

Which works in Firefox, but not IE.  IE always compares them as the
same element.  Here's an example:


    /* Find <td> elements that have "popup" menus */
    var popups = getElementsByClassName(document, 'td', 'popup' );

    var close_menu = function (e) {

        /* Don't close the menu if it's the one that should be open */
        if ( this == current_open )
            return;

        /* Close last open and open this one */
        current_open.className = current_open.className.replace(' over','');

        current_open = this; /* Save for next time */

        this.className += ' over';

        alert('opening ' + this.className );

    }

    /* And add that function to all popup menus */

    for ( var i = 0; i < popups.length; i++ )
        addEvent( popups[i], 'mouseover', close_menu, 'false' );

What's a better way to track that current open menu?




-- 
Bill Moseley
moseley at hank.org




More information about the Javascript mailing list