Rajan Rawal
Rajan Rawal

Reputation: 6317

Nested Ul LI hover hide show element jquery

In nested ul li, I need to show edit and update link on li hover. I have jquery code that does it for me. Jquery works fine when i traverse from top to bottom but when I traverse from bottom to top, it does not work as desired and shows parent li's hidden div.

I want only hover li's span to show. Here is the required.

<ul id="tree">
<li><span>Mobile </span>&nbsp; <span class="links" style="display: none;">Hey Hi, YOu
    caught me!!!</span>
    <ul class="subItem">
        <li><span>GSM Mobiles </span>&nbsp; <span class="links" style="display: none;">Hey Hi,
            YOu caught me!!!</span> </li>
        <li><span>Smart Mobiles </span>&nbsp; <span class="links" style="display: none;">Hey
            Hi, YOu caught me!!!</span>
            <ul class="subItem">
                <li><span>Android Mobiles </span>&nbsp; <span class="links" style="display: none;">Hey
                    Hi, YOu caught me!!!</span> </li>
                <li><span>Sabian Mobiles </span>&nbsp; <span class="links" style="display: none;">Hey
                    Hi, YOu caught me!!!</span> </li>
            </ul>
        </li>
        <li><span>Dual SIM Mobiles </span>&nbsp; <span class="links" style="display: none;">
            Hey Hi, YOu caught me!!!</span> </li>
    </ul>
</li>
<li><span>Watches </span>&nbsp; <span class="links" style="display: none;">Hey Hi, YOu
    caught me!!!</span>
    <ul class="subItem">
        <li><span>Chronograph Watches </span>&nbsp; <span class="links" style="display: none;">
            Hey Hi, YOu caught me!!!</span> </li>
        <li><span>Water Resistance </span>&nbsp; <span class="links" style="display: none;">
            Hey Hi, YOu caught me!!!</span> </li>
    </ul>
</li>
</ul>

My jQuery Code is here:

$('ul li').hover(function () {
            $("ul>li>span.links").hide();
            $(this).find("span.links").first().show();
        }, function () {

            $("ul>li>span.links").hide();
            $(this).find("span.links").first().hide();

        });

Here is jsFiddle Link

Upvotes: 0

Views: 2218

Answers (1)

Jonas Geiregat
Jonas Geiregat

Reputation: 5432

When printing out $(this) I noticed that, you are selecting both the inner and outer lists, be more specific in your selector:

$('#tree ul li').hover(function () {
   $("ul>li>span.links").hide();  

    $(this).find("span.links").first().show();
}, function () {
    $("ul>li>span.links").hide();
    $(this).find("span.links").first().hide();
});

Upvotes: 1

Related Questions