The jQuery filter()
method can be used to filter out all elements from the set of matched elements that do not match the specified selector(s). It can take the selector or a function as its argument to filters the set of matched elements based on a specific criteria. The supplied selector or function to the filter() method is tested against each element in the set of matched elements and all the elements that matching the supplied selector or pass the function's test will be included in the result.
Example
<style type="text/css">
.highlight{
background: yellow;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("ul li").filter(":even").addClass("highlight");
});
</script>
<h2>Unordered List</h2>
<ul>
<li>First list item</li>
<li>Second list item</li>
<li>Third list item</li>
<li>Fourth list item</li>
</ul>
Preview