views:

111

answers:

2

Hi I'm trying to make a function that will add a striped effect to an unordered list, I've got the following so far but cant work out how the selector should work.

    (function($) {
    $.fn.stripe = function(){
        this.$("li:even").css("background-color","#f00");
    };
    })(jQuery);


    $("list_id").stripe();

thnks

+2  A: 
$(this).children('li:even').css("background-color","#f00");
reko_t
Also: $("list_id").stripe(); should be: $("#list_id").stripe();
kaba
+1  A: 

As reko has said, the best way to implement this is using the pseudo odd and even. Try implementing it the same as the following:

JQuery

$(document).ready(function() {
    $("ul.formattedList:odd").addClass("oddItem");
    $("ul.formattedList:even").addClass("evenItem");
}

CSS

.formattedList .oddItem {
    background-color: Black;
    color: White;
}

.formattedList .evenItem {
    background-color: White;
    color: Black;
}

HTML

<ul class="formattedList">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
</ul>
GaryDevenay