jQuery: .filter()

This entry is part 29 of 33 in the series jQuery Tutorial

.filter() reduces the set of matched elements to those that match the selector or pass the function’s test.

<!doctype html>
<html>
<head>
    <title>eq demo</title>
    <script src='//code.jquery.com/jquery-1.10.2.js'></script>
    <style>
        li {
            background: yellow;
        }

        .blue {
            background: blue;
        }
    </style>
</head>
<body>

<ul>
    <li>list item 1</li>
    <li>list item 2</li>
    <li>list item 3</li>
    <li>list item 4</li>
    <li>list item 5</li>
    <li>list item 6</li>
</ul>

<script>
    $('li').filter(':even').addClass('blue');
</script>

</body>
</html>

Try The Code

There are 6 list items in the list and we have used :even selector to select the even number list.

Series Navigation<< jQuery: .eq()
jQuery: siblings >>