DJafari
DJafari

Reputation: 13535

jquery selector

Hi this is my html code :

<ul>
    <li>li 1</li>
    <li>li 2</li>
    <li>li 3</li>
    <li>li 4</li>
    <li>li 5</li>
</ul>

and this is my jquery code :

$('ul li').each(function(index)
{
    if( index > 2 )
    {
        $(this).css('color','red');
    }
});

and this is jsfiddle link : http://jsfiddle.net/YLBcs/

i want to know that exist a other way to do this work without each function ?

Upvotes: 0

Views: 107

Answers (3)

user605334
user605334

Reputation:

SLaks answer quite better. try this one. it's perform faster then SLaks example

$('li:gt(2)','ul').css('color','blue');

Upvotes: 1

manji
manji

Reputation: 47968

$('ul li').slice(3).css('color','red');

Upvotes: 2

SLaks
SLaks

Reputation: 887225

You're looking for the :gt selector:

$('ul li:gt(2)').css('color','red');

Upvotes: 7

Related Questions