<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery Get Text Contents of the Elements</title> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script> $(document).ready(function(){ $(".btn-one").click(function(){ var string = $("p").text(); alert(string); }); $(".btn-two").click(function(){ var string = $("p:first").text(); alert(string); }); $(".btn-three").click(function(){ var string = $("p.extra").text(); alert(string); }); }); </script> </head> <body> <button type="button" class="btn-one">Get All Paragraph</button> <button type="button" class="btn-two">Get First Paragraph</button> <button type="button" class="btn-three">Get Last Paragraph</button> <p>This is a first paragraph.</p> <p>This is a second paragraph.</p> <p class="extra">This is a last paragraph.</p> </body> </html>