Reputation: 6317
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> <span class="links" style="display: none;">Hey Hi, YOu
caught me!!!</span>
<ul class="subItem">
<li><span>GSM Mobiles </span> <span class="links" style="display: none;">Hey Hi,
YOu caught me!!!</span> </li>
<li><span>Smart Mobiles </span> <span class="links" style="display: none;">Hey
Hi, YOu caught me!!!</span>
<ul class="subItem">
<li><span>Android Mobiles </span> <span class="links" style="display: none;">Hey
Hi, YOu caught me!!!</span> </li>
<li><span>Sabian Mobiles </span> <span class="links" style="display: none;">Hey
Hi, YOu caught me!!!</span> </li>
</ul>
</li>
<li><span>Dual SIM Mobiles </span> <span class="links" style="display: none;">
Hey Hi, YOu caught me!!!</span> </li>
</ul>
</li>
<li><span>Watches </span> <span class="links" style="display: none;">Hey Hi, YOu
caught me!!!</span>
<ul class="subItem">
<li><span>Chronograph Watches </span> <span class="links" style="display: none;">
Hey Hi, YOu caught me!!!</span> </li>
<li><span>Water Resistance </span> <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
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