jQuery HTML Element How to - Remove all child nodes (including text nodes) from all paragraphs








Question

We would like to know how to remove all child nodes (including text nodes) from all paragraphs.

Answer


<!-- w  w w .  j ava2  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(){
                  $("p").click(function () {
                      $(this).empty();
                  });
        });
    </script>
  </head>
  <body>
            <p>World</p>
            <div>Replaced!</div>
  </body>
</html>

The code above is rendered as follows: