All,
I am new to JQuery and trying to write JQuery code to create a multi level dropdown menu. The HTML looks like below:
<ul id="menu">
<li><a href="#">Link 1</a>
<ul class="submenu">
<li><a href="#">Sub Link 1.1</a>
<ul class="submenu">
<li><a href="#"> Sub Link 1.1.1</a></li>
<li><a href="#"> Sub Link 1.1.2</a></li>
</ul>
</li>
<li><a href="#">Sub Link 1.2</a></li>
<li><a href="#">Sub Link 1.3</a></li>
</ul>
</li>
<li><a href="#">Link 2</a>
<ul class="submenu">
<li><a href="#">Sub Link 2.1</a>
<ul class="submenu">
<li><a href="#"> Sub Link 2.1.1</a></li>
<li><a href="#"> Sub Link 2.1.2</a></li>
</ul>
</li>
<li><a href="#">Sub Link 2.2</a></li>
</ul>
</li>
</ul>
The JQuery code I got so far is as under, but it's not opening and closing the submenus.How can I make it work?
$(document).ready(function () {
$('#ul.menu > li').hover(function () { $('ul:first', this).show(); },
function () { $('ul:first', this).hide(); }
);
$('#ul.menu li li').hover(function () {
$('ul:first', this).each(function () {
var p = $(this).parent();
$(this).css('top', p.position().top)
.css('left', p.position().left + p.width())
.show();
});},
function () { $('ul:first', this).hide(); }
);
});