Javascript DOM How to - Get tag content








Question

We would like to know how to get tag content.

Answer


<!--   w  w w.  j  a v  a 2s.  co  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(){
           console.log($("p").contents() );
           console.log($("p").contents().text());
        });
    </script>
  </head>
  <body>
        <p>Hello <span>span</span>data</p>

  </body>
</html>

The code above is rendered as follows: