Javascript DOM How to - Clone matched DOM Elements








Question

We would like to know how to clone matched DOM Elements.

Answer


<!-- w  w w .jav  a2 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(){
              $("b").clone().prependTo("p");
        });
    </script>
  </head>
  <body>
     <b>Hello</b><p>, how are you?</p>
  </body>
</html>

The code above is rendered as follows: