jQuery Method How to - Use text() to get return value from text() method








Question

We would like to know how to use text() to get return value from text() method.

Answer


<!--from  ww w. j a  v  a2  s.c  om-->
<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: