Javascript DOM How to - Remove just added elements








Question

We would like to know how to remove just added elements.

Answer


<!--from  w w  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(){
              $("div").hover(
                  function () {
                    $(this).append($("<span> ***</span>"));
                  }, 
                  function () {
                    $(this).find("span:last").remove();
                  }
             );
        });
    </script>
  </head>
  <body>
       <div><h1>header 1</h1></div>
    </body>
</html>

The code above is rendered as follows: