mcan
mcan

Reputation: 2082

Jquery hover with list items

In my html code there are list items. I want to show and hide related fav div when hover the related list item. But i can not relate fav div and it's list item.

html:

<ul>    
<php? for(....): ?>                            
    <li>
        <div id="sin">
            some stuff 
        </div>
        <div id="son">
            some stuff
        </div>
        <div id="fav">
            something 
        </div>
        <br />
    </li>
<php? endfor; ?>
</ul> 

when mouse over <li> tags "fav" div which belongs to mouse overed list item has to appear.

My unworked jquery code:

$(document).ready(function()
{          
    $("li").hover(function(){
        $("#fav").show()},
        function () {
        $("#fav").hide()
    });
});

Upvotes: 0

Views: 2770

Answers (2)

bukart
bukart

Reputation: 4906

here you have

$(document).ready(function()
{
    $('.fav').hide();
    $("li").hover(function(){
        $(".fav",$(this)).show()},
        function () {
        $(".fav",$(this)).hide()
    });
});

you need to use the context with $(".fav",$(this)). instead of $(".fav").. Also you've to change from id to class like

                            <li>
                                <div class="sin">
                                    some stuff
                                </div>

                                <div class="son">
                                    some stuff
                                </div>

                                <div class="fav">
                                    something
                                </div>
                                <br />
                            </li>

Upvotes: 1

StaticVariable
StaticVariable

Reputation: 5283

The problem is that you are using same id multiple times. You need to use class="fav" instead of id="fav"

//for #sin and #son also

$(document).ready(function()
{          
    $("li").hover(function(){
        $("#fav").show()},
        function () {
        $("#fav").hide()
    });
});

should be

$(document).ready(function()
{          
    $("li").hover(function(){
        $(this).find(".fav").show()},
        function () {
        $(this).find(".fav").hide()
    });
});

Upvotes: 1

Related Questions