tags:

views:

31

answers:

3

Hi, all! I want to change css color of all li elements that greater than that the cursor I hover. To be clear I give an example: Suppose I have 10 li - elements in an unordered-list (ul - element).When I hover over 5th element I want that all elements from 5 to 10 will be changed css color (first 4 elements remains black be default). My code is this one, but it doesn't work:

$(function(){
            $("li").hover(
                 function () {                   
                    var l = $("li").index(this);
                    $('div').html(l);
                    $("li:gt(l)").css('color','red');                    
                 }, 
                 function () {
                    $('div').html("");
                    $('li').css('color','black');
                 }
            );
});
A: 

Use nextAll():

$(function(){
        $("li").hover(
             function () {                   
                var l = $("li").index(this);
                $('div').html(l);
                $(this).nextAll('li').andSelf().css('color','red');                    
             }, 
             function () {
                $('div').html("");
                $('li').css('color','black');
             }
        );
});
Felix Kling
thank you! it's also works.
+1  A: 
Alexander Vakrilov
thank you! it's now works
A: 

And I founded one more posibility with slice():

$(function(){
            $("li").hover(
             function () {                   
                var l = $("li").index(this);
                $('div').html(l);
                $('li').slice(l).css('color', 'red');                    
             }, 
             function () {
                $('div').html("");
                $('li').css('color','black');
             }
        );
        });