jQuery eq()
with negative index
<!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").eq(-2).css("background-color", "yellow"); });//from w ww . java 2s. c o m </script> </head> <body> <h1>Welcome to My Homepage</h1> <p>This is a test.</p> <p>CSS.</p> <p>the 2nd element from the end.</p> <p>This is another test.</p> </body> </html>