jQuery Hover event is not bound to the child element

html, javascript, jquery

Solution

The `.hover()` jQuery function takes two arguments, the function to be executed at `hover` (mouseenter) and the function to be executed at `hover out` (mouseleave). You should use that instead of `mouseleave`

groupTab.hover(function () {
        if (!($(this).children("a").hasClass("current"))) {
            $(this).siblings("li").children("a").removeClass("hoverBG");
            $(this).children("a").addClass("hoverBG");
        }
    }, function () {
        $(this).children("a").removeClass("hoverBG");
    });

DEMO

Problem

I bind the hover event to the `li` tag(see my code below or fiddle, for code details), i.e. when i hover over the `li`, i added the class which changes the background of the `li`. `li` tag is having one `a` tag which encloses two `span` tags with texts, when i hover over the two text `span` tags, i am not seeing the hover event is executed. Please see my fiddle http://jsfiddle.net/shmdhussain/JbVMv/ . Thanks in advance for any help. ``` <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Untitled Page</title> <link rel="stylesheet" type="text/css" href="css/reset.css"></link> <link rel="stylesheet" type="text/css" href="css/mystyle.css"></link> <script src="/jquery-my.js" ></script> <script> jQuery(function($){ var groupTab = $("ul").children("li"); groupTab.hover(function(){ if(!($(this).children("a").hasClass("current"))) { $(this).siblings("li").children("a").removeClass("hoverBG"); $(this).children("a").addClass("hoverBG"); } }); groupTab.mouseout(function(){ $(this).children("a").removeClass("hoverBG"); }); }); </script> <style> li{background-color:#DCDEDB; border:1px solid black; padding:20px; } .hoverBG{ background-color:red; } </style> </head> <body> <div class=""> <ul class="" id=""> <li class=" "> <a class="current" href="#" > <span class=""> MyName </span> <br> <span class="">MyData</span> </a> </li> <li class="" > <a class="" href="#" > <span class=""> MyName </span> <br> <span class="">MyData</span> </a> </li> <li class="" > <a class="" href="#"> <span class=""> MyName </span> <br> <span class="">MyData</span> </a> </li> </ul> </div> </body> </html> ```

Original source