views:

29

answers:

1

what is the best way to optimize the following snippets that uses delegate()?

jQuery('.menu').delegate('li.gallery', 'hover', function () { jQuery(this).children('.submenu').toggleClass('hide show'); });
jQuery('.menu').delegate('li.interior', 'hover', function () { jQuery(this).children('.submenu').toggleClass('hide show'); });
jQuery('.menu').delegate('li.exterior', 'hover', function () { jQuery(this).children('.submenu').toggleClass('hide show'); });


<li class="gallery"> 
                    <span>gallery</span> 
                    <ul class="submenu hide"> 
                        <li class="interior"> 
                            <a href="/gallery/interior">Interior</a> 
                            <ul class="submenu hide"> 
                                <li><a href="/gallery/interior?gallery=master-bedroom">Master Bedroom</a></li> 
                                <li><a href="/gallery/interior?gallery=living-room">Living Room</a></li> 
                                <li><a href="/gallery/interior?gallery=dining-room">Dining Room</a></li> 
                                <li><a href="/gallery/interior?gallery=kitchen">Kitchen</a></li> 
                                <li><a href="/gallery/interior?gallery=bathroom">Bathroom</a></li> 
                                <li><a href="/gallery/interior?gallery=foyer">Foyer</a></li> 
                                <li><a href="/gallery/interior?gallery=study">Study</a></li> 
                                <li><a href="/gallery/interior?gallery=sunroom">Sunroom</a></li>                                
                                <li><a href="/gallery/interior?gallery=guest-room">Guest Room</a></li>                                                              
                            </ul> 
                        </li> 
                        <li class="exterior"> 
                            <a href="/gallery/exterior">Exterior</a> 
                            <ul class="submenu hide"> 
                                <li><a href="/gallery/exterior?gallery=landscapes">Landscape</a></li> 
                                <li><a href="/gallery/exterior?gallery=gardens">Gardens</a></li> 
                                <li><a href="/gallery/exterior?gallery=cottages">Cottages</a></li> 
                                <li><a href="/gallery/exterior?gallery=entry-driveway">Entry/Driveway</a></li>                                                                                              
                            </ul> 
                        </li>                       
                    </ul> 
                </li>
+2  A: 

You can use a multiple selector (,) in the .delegate() selector parameter.

However, due to what I would consider a bug in how .delegate() works (off mouseover/mouseout, which fires when entering a child) I'd steer clear it it for now and bind directly, like this:

jQuery('.menu').find('li.gallery, li.interior, li.exterior').hover(function () {
  jQuery(this).children('.submenu').toggleClass('hide show'); 
});
Nick Craver
i thought so too, but when i get i hover over the submenu, the first toggle triggers again. i added the markup above.
matt ryan
@matt - Which version of jQuery are you using? `hover` wasn't added correctly until recently.
Nick Craver
@nick 1.4.2, i think the only version with delegate.
matt ryan
@matt - 1.4.3 made some changes here, just for kicks try 1.4.3?
Nick Craver
@nick yeah it's the same. when hover over the child the parent is getting toggled.
matt ryan
@matt - I see the issue you're having, it's due to the way `.live()` maps to `mouseneter` and `mouseleave` events, one moment.
Nick Craver
@nick, indeed which is why i choose delegate and not live.
matt ryan
@matt - `.delegate()` uses `.live()` underneath, so it's the same issue, you can see the code here: http://github.com/jquery/jquery/blob/master/src/event.js#L930
Nick Craver
@nick oh wow, thanks. do you suggest an alternative technique?
matt ryan
@matt - not at the moment...the jQuery core guys need to fix this one, then the original answer is what I'd go with.
Nick Craver