Mak
Mak

Reputation: 23

JQuery : ul on hover for dropdown

Trying to use jQuery's hover to visible/hide a ul item to give dropdown effect.

Here is the jQuery. Ignore the alert functions

$(document).ready(function () { 

    $('ul.drop li').hover(
        function () {
            alert("asa");
            //show its submenu
            var isVisible = $(this).next('ui').is(':visible');
            alert("dvData is " + isVisible);
            var isHidden = $(this).next('ui').is(':hidden');
            alert("dvData is " + isHidden);

            $(this).next('ul').css({visibility: "visible", display: ""});

        }, 
        function () {
            //hide its submenu
            $(this).next('ul').css({visibility: "hidden", display: ""});
        }
    );

});

Here is the jsp page

<ul class="drop">
<li><a href="#">Main</a></li>
<li>Reporting
 <ul >
    <li><a href="#" target="_blank">Magellan Reports</a></li>
    <li><a href=""/>"  target="_blank">Script Reports</a></li>
 </ul>

<li><a href="<c:url value="#"/>"   target="_blank">CMS</a></li>
<li><a href="<c:url value="#"/>"  target="_blank">Operations</a></li>
</ul>

Here is the css

ul#nav {padding-left:16px;background-image:url(../images/buehne_5_intra.gif)}
clear {clear:both}
ul.drop a {color:#87A7CE;line-height:14px;display:block;text-decoration: none;
background-repeat:no-repeat;background-image:url(../images/diagonale_hauptnavigation.gif);background-position:right;
    height:10px;padding-right:22px; float:left;}
ul.drop, ul.drop li, ul.drop ul { list-style: none; margin: 0; padding: 0; background: #039;
color: #fff;}
ul.drop { position: relative; z-index: 597; float: left; }
ul.drop li { float: left; line-height: 1.0em; vertical-align: middle; zoom: 1; padding: 5px 10px; }
ul.drop li ul { visibility: hidden; position: absolute; top: 100%; left: 20px; z-index: 598; width: 150px; margin-left:50px;}
ul.drop ul a {color:#87A7CE;line-height:14px;display:block;text-decoration: none;height:10px;padding-left:10px; float:left;background-image:none;}
ul.drop ul li { float: none; }
ul.drop ul ul { top: -2px; left: 100%; }
.drop img{width:22px;height:24px;background-color:#039;margin-left:-20px;vertical-align:top}

I cud have done it with css alone, but some required,long, and corrupted css files are not allowing me to do so.. Please help with the jQuery.

Upvotes: 1

Views: 2924

Answers (1)

Goran Mottram
Goran Mottram

Reputation: 6304

next() looks for the next sibling, which matches nothing because you have no <ul> siblings for the triggering <li>. Instead, you'd want to look for the child, so try children() to find matching children. Modify your JS to the following:

$('ul.drop li').hover(
    function () {
        $(this).children('ul').css({visibility: "visible", display: ""});
    }, 
    function () {
        $(this).children('ul').css({visibility: "hidden", display: ""});
    }
);

You can view a demo here - http://jsfiddle.net/GoranMottram/Uxj2B/3/ - The CSS looks like it'll need a bit of work, but at least the JS is working.

Upvotes: 1

Related Questions