Javascript examples for jQuery:Tag Traversing
Add a yellow background color to the last <p> element inside the last <div> element.
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("div p").last().css("background-color", "yellow"); });/*from ww w . j av a2 s.c o m*/ </script> </head> <body> <div style="border: 1px solid black;"> <p>This is the first paragraph in a div.</p> <p>This is the last paragraph in a div.</p> </div><br> <div style="border: 1px solid black;"> <p>This is the first paragraph in another div.</p> <p>This is the last paragraph in another div.</p> </div> </body> </html>