top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to filter out elements from a set of matched elements using jQuery?

0 votes
358 views
How to filter out elements from a set of matched elements using jQuery?
posted Jul 14, 2017 by Ananna Dey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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

enter image description here

answer Jul 14, 2017 by Anand Huded
...