jQuery filter()
by children element with class
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").filter($("div p.intro")).css("background-color", "yellow"); });/*from w ww . java 2 s . c om*/ </script> </head> <body> <div style="border:1px solid black;">This is a div element <h1>Welcome to My Homepage</h1> <p>This is a test.</p> <p class="intro">I am from java2s.com (will be selected).</p> <p>This is another test.</p> </div> <p class="intro">This p element is outside the div element.</p> <p>This p element is also outside the div element.</p> </body> </html>