filter() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:filter

Description

The filter() method gets elements by a certain criteria.

Syntax

ParameterRequire Description
criteria Optional.a selector expression, a jQuery object or one or more elements
function(index)? Optional.a function to run for each element in the set.
  • function(index) returns true, the element is kept. Otherwise, the element is removed.
  • index - The index position of the element in the set

The following code shows how to Return all <p> elements with class name "intro":

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("p").filter(".intro").css("background-color", "yellow");
});/*from ww  w .j  a va2  s.  c  om*/
</script>
</head>
<body>



<p>test</p>
<p class="intro">test</p>
<p class="intro">test</p>
<p>test</p>

</body>
</html>

Related Tutorials