Javascript examples for jQuery:Tag Traversing
Use the children() method to add a red color to the element with class="demo" inside <div> with children(filter)
<!DOCTYPE html> <html> <head> <style> .descendants * {/* www . j a v a2 s . com*/ display: block; border: 2px solid lightgrey; color: blue; padding: 5px; margin: 15px; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("div").children(".demo").css({"color": "red"}); }); </script> </head> <body> <div class="descendants" style="width:500px;">div (parent) <p class="demo">paragraph 1</p> <p>paragraph 2</p> </div> </body> </html>