I have a pretty simple jQuery drop-down menu with links in the normal ul / li list location. The code for the dropdown is as follows:
$('body').ready(function() { if(screen.width <= 720) { $('.dropdown').hover(function() { $(this).find('.subMenu').toggle(); }); $('.dropdown').click(function() { if( $('this').css('display') != 'none') { $('this').toggle(); } }); } else { $('.dropdown').hover( function() { $(this).stop(true,true).find('.subMenu').slideDown("fast"); }, function() { $(this).stop(true,true).find('.subMenu').hide(); } ); } });
On mobile devices (ignore the width of 720, it's just for testing right now), I would like to get the following functions:
Disables communication with the drop-down menu> If the menu is open, close it
Disables communication between the user when another menu is already open. > Close previous menu, open current menu
- The user deletes somewhere else on the page> Close all open menus.
I found that the hover function actually handles # 2 and # 3, but I can't figure out how to get # 1. I'm sure I know why this particular attempt
$('.dropdown').click(function() { if( $('this').css('display') != 'none') { $('this').toggle(); } });
fails. I assume that clicking (or tap in this case) triggers the hover event, which seems to take precedence and therefore displays the menu rather than hiding it.
How can I make this work on mobile devices?
Edit: I am using jQuery v1.7.2
The structure of the HTML list is as follows if it helps someone (shortened version):
<div id="navbar"> <ul> <li class="dropdown"><a href="#">Link A</a></li> <li class="dropdown"><a href="#">Link B</a> <ul class="subMenu"> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> </ul> </li> </ul> </div>
source share