jQuery Selector How to - Select first last paragraph








Question

We would like to know how to select first last paragraph.

Answer


<!-- w  w w  .java  2 s.  c o  m-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
               var str = $("p:first").text()+"added";
               $("p:last").html(str);
        });
    </script>
  </head>
  <body>
         <p>asdf</p>
         <p>asdf</p>
         <p>asdf</p>
         <p>asdf</p>
  </body>
</html>

The code above is rendered as follows: